关于qt:Y-分钟速成-Qt-Framework

4次阅读

共计 2429 个字符,预计需要花费 7 分钟才能阅读完成。

源代码下载:learnqt-cn.cpp
Qt Qt 是一个广为人知的框架,用于开发跨平台软件,该软件能够在各种软件和硬件平台上运行,代码简直没有变动,同时具备本机应用程序的能力和速度。尽管 Qt 最后是用C++,但也有其余语言的端口: PyQt, QtRuby, PHP-Qt, 等等.

Qt 非常适合应用图形用户界面(GUI)创立应用程序。本教程是对于如何用 C++ 去实现。

/*
 * 让咱们从最经典的开始
 */

// Qt 框架的所有标头均以大写字母 'Q' 结尾
#include <QApplication>
#include <QLineEdit>

int main(int argc, char *argv[]) {
     // 创立一个对象来管理应用程序范畴内的资源
    QApplication app(argc, argv);

    // 创立行编辑 widgets 并在屏幕上显示
    QLineEdit lineEdit("Hello world!");
    lineEdit.show();

    // 启动应用程序的事件循环
    return app.exec();}

Qt与 GUI 相干的局部与 widgets 及其之间的 connection 无关。

浏览更多无关 widgets 的信息

/*
 * 让咱们创立一个标签和一个按钮。* 按下按钮时应显示一个标签。* Qt 代码自身就能够阐明问题。*/
 
#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>

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

    QDialog dialogWindow;
    dialogWindow.show();
    
    // 增加垂直布局
    QVBoxLayout layout;
    dialogWindow.setLayout(&layout);  

    QLabel textLabel("Thanks for pressing that button");
    layout.addWidget(&textLabel);
    textLabel.hide();

    QPushButton button("Press me");
    layout.addWidget(&button);
    
    // 按下按钮时显示暗藏标签
    QObject::connect(&button, &QPushButton::pressed,
                     &textLabel, &QLabel::show);

    return app.exec();}

留神,QObject :: connect局部。此办法用于将一个对象的 SIGNAL 连贯到另一个对象的SLOTS

Signals 会被收回当对象产生某些事件时,例如当用户按下 QPushButton 对象时会收回 push 的信号。

Slots 是能够响应于接管到的信号而执行的action

浏览无关 SLOTS 和 SIGNALS 的更多信息

接下来,让咱们理解到咱们不仅能够应用规范的 wigets,而且能够通过继承扩大其行为。让咱们创立一个按钮并计算其被按下的次数。为此,咱们定义了本人的类 CounterLabel 。因为特定的 Qt 体系结构,必须在独自的文件中申明它。

// counterlabel.hpp

#ifndef COUNTERLABEL
#define COUNTERLABEL

#include <QLabel>

class CounterLabel : public QLabel {
    Q_OBJECT  // 在每个自定义 wiget 中必须存在的 Qt 定义的宏

public:
    CounterLabel() : counter(0) {setText("Counter has not been increased yet");  // QLabel 办法
    }

public slots:
    // 将响应按钮按下而调用的操作
    void increaseCounter() {setText(QString("Counter value: %1").arg(QString::number(++counter)));
    }

private:
    int counter;
};

#endif // COUNTERLABEL
// main.cpp
// 与后面的示例简直雷同

#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QString>
#include "counterlabel.hpp"

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

    QDialog dialogWindow;
    dialogWindow.show();

    QVBoxLayout layout;
    dialogWindow.setLayout(&layout);

    CounterLabel counterLabel;
    layout.addWidget(&counterLabel);

    QPushButton button("Push me once more");
    layout.addWidget(&button);
    QObject::connect(&button, &QPushButton::pressed,
                     &counterLabel, &CounterLabel::increaseCounter);

    return app.exec();}

当然,Qt 框架比本教程介绍的局部要简单得多,因而请仔细阅读和练习。

进一步浏览

  • Qt 4.8 tutorials
  • Qt 5 tutorials

祝你好运,生存欢快!


有倡议?或者发现什么谬误?在 Github 上开一个 issue,或者发动 pull request!

原著 Aleksey Kholovchuk,并由 0 个好心人批改。
© 2022 Aleksey Kholovchuk
Translated by: GengchenXU
本作品采纳 CC BY-SA 3.0 协定进行许可。

正文完
 0