tags:

views:

39

answers:

1

Hey,

I tried to create a Mainwindow with an slot, which creates a Widget and loads it to the ScrollArea in the Mainwindow. This doesn't work, so I tired to create the Widget in the constructor of the mainwindow and I always get errors and don't know why.. so what's the right declaration of the Widget?

#include <QtGui>


class Mainwindow : public QMainWindow
{
    Q_OBJECT

public:
    Mainwindow(QMainWindow *parent = 0);

public slots:

private:
    QScrollArea *List,*Sublist,*Overall,*Settings;
    QLabel *label_title;
    QPushButton *bn_exit,*bn_list,*bn_overall,*bn_settings;
};

//! ------------------------------------- Mainlist -------------------------------------
class Sublist : public QWidget{
 Q_OBJECT
private:
    QLabel *title;
public:
    Sublist(QWidget *parent = 0);
};

and .cpp

Mainwindow::Mainwindow(QMainWindow *parent) : QMainWindow(parent) {

    this->resize(1024,576);
    //this->setWindowFlags(Qt::Popup);
    QPalette palette;
    palette.setColor(QPalette::Background, QColor(16,16,16));
    this->setPalette(palette);

    Sublist SecondList;

    //! [Set ScrollAreas]
    List = new QScrollArea(this);
    List->setGeometry(0,60,200,456);
    List->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    Sublist = new QScrollArea(this);
    Sublist->setGeometry(200,60,824,456);
    Sublist->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    //Sublist->setWidget(SecondList)

}

//! ---------------------------------------- MainList------------------------------------------------------
Sublist::Sublist(QWidget *parent){
    resize(1200,1200);
    title = new QLabel("Title",this);
    title->setGeometry(1120,1120,40,90);
}
A: 

I have played around with your code a bit, noticed a few things:

In class Mainwindow you define your QScrollArea variables:

QScrollArea *List,*Sublist,*Overall,*Settings;


You define a variable named Sublist of type QScrollArea, but you also have a class of the same name:

class Sublist : public QWidget


Probably would be a good idea to change the variable names for your scroll areas:

QScrollArea *list, *subList, *overall, *settings;


Next, in the constructor for class Sublist you pass a pointer to the parent class but never assign it to your base class. You also have a QLabel widget that never is placed anywhere. What seems to be needed is a layout for your custom widget.


The Sublist class could be something like this:

//.h
class Sublist : public QWidget
{
    Q_OBJECT

public:
    Sublist(QWidget *parent = 0);

private:
    QLabel *title;
    QVBoxLayout *layout;
};

//.cpp
Sublist::Sublist(QWidget *parent) : QWidget(parent) {
    resize(1200,1200);
    title = new QLabel("Title");
    title->setGeometry(1120,1120,40,90);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(title);
    setLayout(layout);
}


The Mainwindow class something like this:

//.h
class Mainwindow : public QMainWindow
{
    Q_OBJECT

public:
    Mainwindow(QMainWindow *parent = 0);

private:
    Sublist *secondList;
    QScrollArea *list, *subList, *overall, *settings;
    QLabel *label_title;
    QPushButton *bn_exit,*bn_list,*bn_overall,*bn_settings;
};

//.cpp
Mainwindow::Mainwindow(QMainWindow *parent) : QMainWindow(parent) 
{
    this->resize(1024,576);
    QPalette palette;
    palette.setColor(QPalette::Background, QColor(16,16,16));
    palette.setColor(QPalette::Foreground, QColor(255,255,255));//set text to white
    this->setPalette(palette);

    secondList = new Sublist(this);

    //! [Set ScrollAreas]
    list = new QScrollArea(this);
    list->setGeometry(0,60,200,456);
    list->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    subList = new QScrollArea(this);
    subList->setGeometry(200,60,824,456);
    subList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    subList->setWidget(secondList);
}


Im still not 100% sure this is what you where trying to achieve with this code, but I hope that I have helped you to resolve some of the issues in your current implementation.

Adam
you were right, the name of the class and variable were the same, this fixed the problem. thank you!
Alex