我尝试创建一个带有插槽的Mainwindow,它创建一个Widget并将其加载到Mainwindow中的ScrollArea。这不起作用,所以我厌倦了在主窗口的构造函数中创建Widget,我总是得到错误,不知道为什么..那么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);
};
和.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);
}
答案 0 :(得分:1)
我已经玩过你的代码了,注意了一些事情:
在课程 主窗口 中,您可以定义 QScrollArea 变量:
QScrollArea *List,*Sublist,*Overall,*Settings;
您定义了一个名为 子列表 的变量 QScrollArea ,但您还有一个同名的班级:
class Sublist : public QWidget
更改滚动区域的变量名称可能是个好主意:
QScrollArea *list, *subList, *overall, *settings;
接下来,在类 子列表 的构造函数中,您传递指向父类的指针,但从不将其指定给您的基类。您还有一个永远不会放在任何地方的QLabel小部件。似乎需要的是自定义小部件的布局。
< 子列表 类可能是这样的:
//.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);
}
< Mainwindow 类如下:
//.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);
}
我仍然不能100%确定这是您尝试使用此代码实现的目标,但我希望我已帮助您解决当前实施中的一些问题。