1.1 创建第一个 Qt 程序 - Hello Qt

  1. 打开 Qt Creator。
  2. 单击“New File or Project”,新建一个项目。
  3. 选择空的 Qt 项目,并命名项目,指定路径。
  4. 在菜单栏中选择“窗体->显示边栏”,查看项目文件夹。
  5. 右键项目文件夹,点击“Add New …”。
  6. 选择 C++ -> C++ Source File,并命名为 main.cpp。
  7. 双击 hello.cpp 文件,编辑源代码如下:
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QLabel *label = new QLabel('Hello Qt!');
    label->show();
    return app.exec();
}
  1. 单击左下角的绿色三角形按钮(或 Ctrl+R)运行程序。
  2. 出现保存对话框时,选择“Save All”。
  3. 运行结果将在窗口中显示“Hello Qt!”。
  4. label->show(); 前添加代码 label->resize(300, 100); 可以调整窗口大小。
  5. 将第 7 行代码改为 QLabel *label = new QLabel('<h2><i>Hello</i> <font color=red>Qt!</font></h2>'); 可以显示格式化的文字。

1.2 创建一个对话框 - Find

  1. 新建一个空的 Qt 项目。
  2. 右键项目文件夹,点击“Add New …”,新建头文件 finddialog.h 和源文件 finddialog.cpp。
  3. 在 finddialog.h 中编辑以下代码:
#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QDialog>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QVBoxLayout>

class FindDialog : public QDialog
{
    Q_OBJECT

public:
    explicit FindDialog(QWidget *parent = 0);
    signals:
        void findNext(const QString &text, Qt::CaseSensitivity cs);
        void findPrevious(const QString &text, 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
  1. 在 finddialog.cpp 中编辑以下代码:
#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(const QString &)),
            this, SLOT(enableFindButton(const 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());
}
  1. 在 main.cpp 中编辑以下代码:
#include <QApplication>
#include "finddialog.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    FindDialog *dialog = new FindDialog;
    dialog->show();
    return app.exec();
}
  1. 在工程文件中编辑以下代码:
TEMPLATE      = app
HEADERS       = finddialog.h
SOURCES       = finddialog.cpp \
                main.cpp
  1. 运行程序,在文本框中输入内容,点击“Find”按钮,由于未添加其他操作,按钮暂时没有实际效果。

实验心得

本次实验学习了 Qt Creator 的基本使用方法,包括新建项目、添加源文件、编辑源代码、运行程序等。通过创建简单的“Hello Qt”程序和“Find”对话框程序,掌握了 Qt 的基本控件和信号槽的使用方法。需要注意代码的编写和调试,以及对界面的布局和样式的调整。本次实验为后续的 Qt 编程打下了基础。

Qt 入门教程:创建第一个 Qt 程序和自定义对话框

原文地址: https://www.cveoy.top/t/topic/lbUd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录