在QMain窗口上添加QT布局的代码
Adding Layout in QT on QMain WIndow using a code
我需要一些帮助,我需要为我的应用程序设置布局,但我不知道如何在QmainWindow上设置布局。
下面是我的一部分window.cpp:
window::window(QWidget *parent)
: QMainWindow(parent)
{
createFilesTable();
queryopen();
exitButton = createButton("E&xit",SLOT(programout()));
insertButton = createButton("&Add", SLOT(insert()));
editButton = createButton("&Edit", SLOT(edit()));
clearButton = createButton("&Clear", SLOT(clear()));
selectButton = createButton("&Select", SLOT(select()));
QHBoxLayout *buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch();
buttonsLayout->addWidget(selectButton);
buttonsLayout->addWidget(insertButton);
buttonsLayout->addWidget(editButton);
buttonsLayout->addWidget(clearButton);
buttonsLayout->addWidget(exitButton);
txtid = new QLineEdit;
txtname = new QLineEdit;
txtdesc = new QLineEdit;
label1 = new QLabel("ID:");
label2 = new QLabel("Name:");
label3 = new QLabel("Description:");
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(label1,1,0,1,1);
mainLayout->addWidget(txtid,1,1,1,2);
mainLayout->addWidget(label2,2,0,1,1);
mainLayout->addWidget(txtname,2,1,1,3);
mainLayout->addWidget(label3,3,0,1,1);
mainLayout->addWidget(txtdesc,3,1,1,3);
mainLayout->addLayout(buttonsLayout,4,1,1,3);
mainLayout->addWidget(filesTable,6,0,6,5);
setLayout(mainLayout);
setWindowTitle("Database Connection");
resize(450,300);
}
对于QMainWindow
,您使用setCentralWidget(QWidget*),不像所有其他QWidget
子类。
原因是QMainWindow
已经有自己的布局,其中包括菜单栏,状态栏,dock窗口等的位置。因此,您创建另一个QWidget
并将其布局设置为所需的布局,然后使QWidget
成为QMainWindow
的中心小部件。
()这种不一致已经让我好几次…但一旦你明白了发生了什么,它就有意义了)
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 是C 单继承的内存布局与此C代码相同
- 这是可移植C++代码吗?(假设多维数组具有连续的内存布局)
- 将组件添加到qt创建者从代码中创建的布局中
- 将应用程序的主窗口 GUI 布局更改为在代码中
- 如何布局C++代码:Visual Studio 2010 中的命名空间和类似文件名
- 在QMain窗口上添加QT布局的代码