以编程方式进行 Qt GUI 设计 [英] Qt GUI design programmatically

查看:24
本文介绍了以编程方式进行 Qt GUI 设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 GUI 应用程序.

I'm try to create a GUI application.

主窗口,一个 QMainWindow,包含 9 个固定大小的标签以及主窗口的大小.

The main window, a QMainWindow, contains 9 labels with fixed size and also the size of the main window.

我尝试在没有 Qt GUI Designer 的情况下以编程方式制作它.该项目的构建没有错误,但我看不到主窗口上显示的任何标签或布局.它只是空白.

I tried to make it programmatically without Qt GUI Designer. The project is built without error but I cannot see any label nor layout shown on the main window. it's just blank.

这是我的源代码:

WCwindow::WCwindow()
{
   // initialize widgets with text
   CAM111 = new QLabel("CAM 01");
   CAM121 = new QLabel("CAM 02");
   CAM131 = new QLabel("CAM 03");

   CAM211 = new QLabel("CAM 04");
   CAM221 = new QLabel("CAM 05");
   CAM231 = new QLabel("CAM 06");

   CAM311 = new QLabel("CAM 07");
   CAM321 = new QLabel("CAM 08");
   CAM331 = new QLabel("CAM 09");

   CAM111->setFixedSize(wcW,wcH);
   CAM121->setFixedSize(wcW,wcH);
   CAM131->setFixedSize(wcW,wcH);
   CAM211->setFixedSize(wcW,wcH);
   CAM221->setFixedSize(wcW,wcH);
   CAM231->setFixedSize(wcW,wcH);
   CAM311->setFixedSize(wcW,wcH);
   CAM321->setFixedSize(wcW,wcH);
   CAM331->setFixedSize(wcW,wcH);

   QGridLayout *layout = new QGridLayout;
   layout->addWidget(CAM111,0,0);
   layout->addWidget(CAM121,0,1);
   layout->addWidget(CAM131,0,2);

   layout->addWidget(CAM211,1,0);
   layout->addWidget(CAM221,1,1);
   layout->addWidget(CAM231,1,2);

   layout->addWidget(CAM311,2,0);
   layout->addWidget(CAM321,2,1);
   layout->addWidget(CAM331,2,2);

   setLayout(layout);

   setWindowTitle("Camera Window");
   setFixedSize(1000, 800);

}

当然,类是在main.cpp中初始化和调用的:

of course, the class is initialized and evoked in main.cpp:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    WCwindow *WCwin = new WCwindow;

    WCwin->show();

    return app.exec();
}

我有什么样的错误??

推荐答案

下面的代码工作正常.问题出在您没有显示的代码中.当您使用 QMainWindow 时,正如您最终承认的那样,您需要使用您构建的新小部件设置其 centralWidget.

The code below works fine. The problem was in the code you weren't showing. When you use a QMainWindow, as you've eventually admitted to doing, you need to set its centralWidget with a new widget that you construct.

// main.cpp
#include <QVector>
#include <QMainWindow>
#include <QLabel>
#include <QGridLayout>
#include <QApplication>

class WCwindow : public QMainWindow
{
public:
    WCwindow();
private:
    QVector<QLabel*> cams;
    QLabel* cam(int r, int c) const {
        return cams[r*3 + c];
    }
};

WCwindow::WCwindow()
{
    QGridLayout *layout = new QGridLayout;

    for (int i = 1; i < 10; ++ i) {
        QLabel * const label = new QLabel(QString("CAM %1").arg(i, 2, 10, QLatin1Char('0')));
        label->setFixedSize(200, 50);
        layout->addWidget(label, (i-1) / 3, (i-1) % 3);
        cams << label;
    }

    QWidget * central = new QWidget();
    setCentralWidget(central);
    centralWidget()->setLayout(layout);

    setWindowTitle("Camera Window");
    setFixedSize(1000, 800);
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    WCwindow win;
    win.show();
    return app.exec();
}

这篇关于以编程方式进行 Qt GUI 设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆