【问题标题】:Qt programs errors with testQt 程序错误与测试
【发布时间】:2014-05-14 18:56:29
【问题描述】:

我正在尝试从 qt 书中编译对话程序。我不断收到此错误:

:'QLabel' : 类没有构造函数

专业人士:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    finddialog.cpp

HEADERS  += mainwindow.h \
    finddialog.h

FORMS    += mainwindow.ui

主要:

#include <QApplication>
#include "finddialog.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    FindDialog *dialog = new FindDialog;
    dialog->show();
    return app.exec();
}

标题:

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QDialog>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QWidget;

class FindDialog : public QDialog
{
    Q_OBJECT
public:
    FindDialog(QWidget *parent = 0);
signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots:
    void findClicked();
    void enableFindButton(const QString &text);
private:
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
};

#endif // FINDDIALOG_H

实施:

#include <QtGui>

#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
    label = new QLabel(tr("Find &what:"));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);

    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("Search &backward"));

    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);

    closeButton = new QPushButton(tr("Close"));

    connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableFindButton(QString)));
    connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);

    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);

    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch();

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("Find"));
    setFixedHeight(sizeHint().height());
}

void FindDialog::findClicked()
{
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs =
            caseCheckBox->isChecked() ? Qt::CaseSensitive
                : Qt::CaseInsensitive;
    if (backwardCheckBox->isChecked()) {
        emit findPrevious(text, cs);
    } else {
        emit findNext(text, cs);
    }
}

void FindDialog::enableFindButton(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());
}

【问题讨论】:

  • 你在哪一行得到这个?另外,我看不到您的主窗口将在哪里使用。另外,你是用 Qt 4 还是 Qt 5 构建的?这本书是为 Qt 4 编写的,因此如果您尝试针对 Qt 5 构建它,它将无法工作。
  • 我认为应该是#include &lt;QLabel&gt;而不是class QLabel;
  • 我使用的是书中完全相同的代码。
  • @System.exit: 不,前向声明很好,因为它不必知道标题中的表示。
  • 我使用的是 qt 版本 5

标签: c++ qt qt5 qlabel qtwidgets


【解决方案1】:

:'QLabel' : 类没有构造函数

您似乎包含 QtGui 而不是 QtWidgets。替换这一行:

#include <QtGui>

#include <QtWidgets>

如果您使用 Qt 5 进行构建。这本书是为 Qt 4 编写的,并且小部件类已移至它们自己的模块中。

免责声明:包含整个模块是个坏主意。

【讨论】:

  • #include 我将 qtgui 替换为 qtwidgets C:\Users\x\Downloads\qt-book-examples\qt-book\chap02\find\finddialog.cpp:1: error: C1083 :无法打开包含文件:'QtWidgets':没有这样的文件或目录
  • @runners3431:您也必须安装小部件模块。确保它安装在其他 Qt dll 文件旁边。还要检查标题在类似标题旁边是否可用。还要检查编译器行是否传递了正确的包含路径。我在发布之前在 Linux 上对此进行了测试,它对我有用。
  • 我最初是通过在线安装程序安装的。我如何安装模块。使用窗户
  • 应该已经安装了 widgets 模块,但请仔细检查它,以及构建时是否将包含路径正确传递给编译器。如果您使用正确安装的 qmake,它应该可以正常工作。
【解决方案2】:

当用 Qt 4 编写的程序在 Qt 5 中编译时会出现此问题

解决办法是:

  1. 在 .pro 文件中添加以下行:

QT += 核心 gui 小部件

  1. 包括以下头文件:

#include &lt;QtWidgets&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    相关资源
    最近更新 更多