gpt4 book ai didi

c++ - 在 QMainWindow 中创建 Widget 并加载到 ScrollArea

转载 作者:行者123 更新时间:2023-11-28 08:27:08 24 4
gpt4 key购买 nike

我尝试创建一个带有插槽的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);
}

最佳答案

我试了一下你的代码,注意到了一些事情:

Mainwindow 类中定义您的 QScrollArea 变量:

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


您定义了一个名为 Sublist 的变量,类型为 QScrollArea,但您也有一个同名类:

class Sublist : public QWidget


更改滚动区域的变量名可能是个好主意:

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


接下来,在 Sublist 类的构造函数中,您将一个指针传递给父类,但永远不会将其分配给您的基类。您还有一个永远不会放置在任何地方的 QLabel 小部件。似乎需要的是自定义小部件的布局。


Sublist 类可能是这样的:

//.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% 确定这就是您尝试使用此代码实现的目标,但我希望我已经帮助您解决了当前实现中的一些问题。

关于c++ - 在 QMainWindow 中创建 Widget 并加载到 ScrollArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3602096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com