QT Basic Operations

Posted by bobthedog on Thu, 01 Aug 2019 11:15:58 +0200

First, QT is a cross-platform application framework of c++ GUI.

Application Framework: When encapsulated class libraries need to use some functions, they can call the corresponding interfaces directly.

II. Establishment of QT Project

1. New Project - > Application - > QT Widgets Application - > Select - > Name (not including Chinese name) - - > Selection of base class;

Base Class: QMainWindow: Mainly for PC side, with menu bar;

QDialog: Dialog (not scalable);

QWidget: Window control (scalable);

2. The characteristics of the QT header file:

  1. No. h header file;
  2. The header file is the same as the class name.

Both begin with Q and the first two letters are capitalized.

3. It is necessary to specify the parent object between the two controls, otherwise they are two independent windows without any connection.

There are two ways to specify a parent object:

  1. Obj. setParent (& Parent); // Specifies the parent object of obj, passing in the parent object address as a parameter
  2. Pass parameters through constructor: QPushButton B (& Parent);

The advantage of specifying a parent object is that only the parent object is displayed, and the child objects above are automatically displayed.

4. Signal and slot: interface for communication between QT objects

connect(&b, &QPushButton::pressed, this, &Widget::close);

Four parameters are pointer type. The first parameter is the signal sender, the second parameter is the signal to be sent, the third parameter is the signal receiver, and the fourth signal is the slot function. The received signal is processed accordingly (either a library function or a self-defined function).

 

Two examples of window switching:

Main window header file:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include "subwidget.h"

class Widget : public QWidget
{
    Q_OBJECT   //Macro Statement, Signal and Slot Function Requirements
private:
    QPushButton b1;
    subWidget w;
public:
    Widget(QWidget *parent = 0);
    ~Widget();

    void changWindow();
    void deal();
};

#endif // WIDGET_H

Subwindow header file:

subwidget.h

#ifndef SUBWIDGET_H
#define SUBWIDGET_H

#include <QWidget>
#include <QPushButton>

class subWidget : public QWidget
{
    Q_OBJECT

private:
    QPushButton b;
public:
    explicit subWidget(QWidget *parent = 0);

    void sendslot();
signals:
    /* Signals must be declared by signals keywords (custom signals)
     * The signal has no return value, but it can have parameters
     * A signal is a declaration of a function, just a declaration, without definition.
     * Use: emit MySignal();
     */
    void mysignal();

public slots:
};

#endif // SUBWIDGET_H

main.cpp

#include "widget.h"
#Include < QApplication >// Application class

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);   //Objects with and without only one application class

    Widget w;    //Inherited from QWidget, QWidget is a window base class, so Widget is also a window class.      
    w.show();    //w is a window. The created window is hidden by default and needs to be displayed artificially.

    return a.exec();        
    //Equivalent to a.exec(); return 0; Function: Let the program be executed all the time, waiting for user action, that is, waiting for events to occur.
}

Implementation of the main window constructor:

widget.cpp

#include "widget.h"
#include <QPushButton>

Widget::Widget(QWidget *parent): QWidget(parent)
{
    resize(400,300);            //Setting Form Size
    setWindowTitle("main");     //Set the name of the window. If not, the default is the project name.
    b1.setText("Switch to subwindows");  //Set the name of the key display
    b1.setParent(this);         //Specify the parent of the key

    connect(&b1, &QPushButton::clicked, this, &changWindow);
    connect(&w, &subWidget::mysignal, this, &Widget::deal);
    //Since subclasses do not contain objects of the parent class, they can only use custom signals to establish connections and switch back from the child window to the main window.

Widget::~Widget()
{

}

void Widget::changWindow()
{
    hide();
    w.show();
}

void Widget::deal()
{
    w.hide();
    show();
}

Implementation of the sub-window constructor:

subwedget.cpp

#include "subwidget.h"

subWidget::subWidget(QWidget *parent) : QWidget(parent)
{
    resize(400,300);
    setWindowTitle("minor");
    b.setText("Switch to main window");
    b.setParent(this);

    connect(&b, &QPushButton::pressed, this, &sendslot);
}

void subWidget::sendslot()
{
    emit mysignal();
}

 

Topics: Qt Windows