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

源代码下载: 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 协定进行许可。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理