小部件未显示在基本 Qt 应用程序 (QMainWindow) 中 [英] Widgets are not shown in basic Qt application (QMainWindow)

查看:57
本文介绍了小部件未显示在基本 Qt 应用程序 (QMainWindow) 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Qt 的新手,我正在用简单的例子做一些练习.

我只是想通过一个简单的应用程序来测试我的知识,通过编码,用户在 QLineEdit 小部件中键入文本,文本将显示在 QLabel 中.没有必要让它有用.我只是想试试.

在编译应用程序时,我没有收到任何错误.但是,当窗口打开时,QLabel 和 QLineEdit 小部件不可见.

我的代码在这里:

Window.h

#ifndef WINDOW_H#define WINDOW_H#include 类 QGridLayout;QLabel 类;类 QLineEdit;类窗口:公共 QMainWindow{Q_OBJECT上市:显式窗口(QWidget *parent = 0);私人的:QGridLayout *mainLayout;QLabel *标签;QLineEdit *lineEdit;};#endif//WINDOW_H

Window.cpp

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

ma​​in.cpp

#include #include "Window.h"int main(int argc, char *argv[]){QApplication app(argc, argv);窗户窗户;window.show();返回 app.exec();}

我在代码中找不到任何错误.

谢谢,提前.

解决方案

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);
}

这篇关于小部件未显示在基本 Qt 应用程序 (QMainWindow) 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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