且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

小部件未显示在基本 Qt 应用程序 (QMainWindow) 中

更新时间:2023-11-14 17:16:52

QMainWindow 必须有一个中间小部件,即使它只是一个占位符.另请注意,它具有用于添加工具栏、菜单栏等的自己的布局 - 因此您可能希望为中间小部件设置布局(mainLayout 在您的代码中).

查看QMainWindow 类参考了解详情.>

要使您的小部件在主窗口中可见,您可以像这样修改构造函数:

窗口.cpp

#include "Window.h";#include #include #include 窗口::窗口(QWidget *parent): QMainWindow(父){QWidget* someWidget = new QWidget(this);mainLayout = 新的 QGridLayout;label = new QLabel(tr("Text"));lineEdit = 新 QLineEdit;mainLayout->addWidget(label, 0, 0);mainLayout->addWidget(lineEdit, 1, 0);someWidget->setLayout(mainLayout);连接(行编辑,信号(文本更改(QString)),标签,插槽(setText(QString)));setCentralWidget(someWidget);}

I am new to Qt and I am doing some practice with simple examples.

I just wanted to test my knowledge with a simple application, by coding, in which user types a text in QLineEdit widget and the text will be shown in QLabel. There is no need for it to be useful. I just want to try.

While compiling the application, I get no errors. However, QLabel and QLineEdit widgets are not visible when the window is opened.

My codes are here:

Window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QMainWindow>

class QGridLayout;
class QLabel;
class QLineEdit;

class Window : public QMainWindow
{
    Q_OBJECT

public:
    explicit Window(QWidget *parent = 0);

private:
    QGridLayout *mainLayout;
    QLabel *label;
    QLineEdit *lineEdit;
};

#endif // WINDOW_H

Window.cpp

#include "Window.h"
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>

Window::Window(QWidget *parent)
    : QMainWindow(parent)
{
    mainLayout = new QGridLayout;
    label = new QLabel(tr("Text"));
    lineEdit = new QLineEdit;

    mainLayout->addWidget(label, 0, 0);
    mainLayout->addWidget(lineEdit, 1, 0);
    setLayout(mainLayout);

    connect(lineEdit, SIGNAL(textChanged(QString)),
            label, SLOT(setText(QString)));
}

main.cpp

#include <QApplication>
#include "Window.h"

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

    Window window;
    window.show();

    return app.exec();
}

I couldn't find any mistake in the code.

Thanks, in advance.

A QMainWindow must have a central widget, even if it's just a placeholder. Also note that it has its own layout for adding toolbars, menubars, etc. - so you probably want to set the layout (mainLayout in your code) for the central widget instead.

Check the QMainWindow class reference for details.

To make your widgets visible in the main window, you could modify your constructor like this:

Window.cpp

#include "Window.h"
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>

Window::Window(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget* someWidget = new QWidget(this);
    mainLayout = new QGridLayout;
    label = new QLabel(tr("Text"));
    lineEdit = new QLineEdit;

    mainLayout->addWidget(label, 0, 0);
    mainLayout->addWidget(lineEdit, 1, 0);
    someWidget->setLayout(mainLayout);

    connect(lineEdit, SIGNAL(textChanged(QString)),
             label, SLOT(setText(QString)));

    setCentralWidget(someWidget);
}