QWidget: the basic user interface class in Qt

QWidget: the basic user interface class in Qt

The QString class is a very important class in Qt, used to represent basic user interface components. WidgetIt is the base class for other user interface ( ) classes, such as QMainWindow, QDialog, QPushButton, etc. In this article, we will introduce the functions and application scenarios of the QWidget class in detail.

Create QWidget object

To create a QWidget object, we can use the QWidget's default constructor or a constructor that specifies its parent object, name, and other properties. Here, let's take a look at how to create a simple QWidget object using the default constructor:

#include <QWidget>
#include <QApplication>

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

    QWidget widget;                // 默认的QWidget实例
    widget.show();

    return app.exec();
}

In this example, we create a new QWidget instance named "widget" using QWidget's default constructor and make it visible on the screen by calling its show() method.

QWidget properties and methods

QWidget has many properties and methods, here we list some of the most commonly used and important ones:

  • resize(): Set the size of QWidget.
  • move(): Moves the QWidget to the given window coordinates.
  • show(): Display QWidget.
  • hide(): Hide QWidget.
  • setWindowTitle(): Set the window title.
  • setWindowIcon(): Set the window icon.
  • setLayout(): Sets the layout manager of QWidget.

For example, in the following code, we show how to create a QWidget interface and set the title, size, and layout using:

#include <QWidget>
#include <QHBoxLayout>
#include <QApplication>

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

    QWidget widget;
    widget.setWindowTitle("My Widget");
    widget.resize(400, 300);

    QHBoxLayout* layout = new QHBoxLayout;
    QLabel* label = new QLabel("Hello World", &widget);
    layout->addWidget(label);
    widget.setLayout(layout);

    widget.show();

    return app.exec();
}

In this example, we first create a new QWidget instance "widget" and set its title to "My Widget" and size to 400x300. We then created a horizontal layout and added a label containing the text "Hello World" to it. Finally, we set that layout as the QWidget's layout manager and make the QWidget visible on the screen.

QWidget event handling

All QWidgets have event handling methods. We can use these methods to respond to events from the system and user interface, such as window redraws, mouse clicks, etc. These methods allow us to write custom event handlers to control the behavior of the user interface.

Commonly used QWidget event handling methods are:

  • mousePressEvent(QMouseEvent *event): Mouse press event.
  • mouseMoveEvent(QMouseEvent *event): Mouse move event.
  • keyPressEvent(QKeyEvent *event): Keyboard press event.
  • resizeEvent(QResizeEvent *event): Window resize event.

For example, in the following code, we show how to implement QWidget's event handling:

#include <QWidget>
#include <QMouseEvent>
#include <QApplication>

class MyWidget : public QWidget {
    
    
public:
    MyWidget() {
    
     }

protected:
    void mousePressEvent(QMouseEvent *event) override {
    
    
        qDebug() << "Mouse pressed at:" << event->pos();
    }
};

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

    MyWidget widget;
    widget.resize(400, 300);
    widget.show();

    return app.exec();
}

In this example, we first create a new "MyWidget" class derived from QWidget and override its mousePressEvent() method to handle the mouse press event and output the coordinates of the click location through qDebug(). Finally, we create a MyWidget object named "widget" and make it visible.

in conclusion

In Qt applications, QWidget is the base class and parent class for all user interfaces. It provides many properties and methods that help us easily create, set up and respond to user interfaces. In addition to this, QWidget also provides a powerful event handling system, allowing us to write custom event handlers to control the behavior of the application.

Guess you like

Origin blog.csdn.net/qq_25549309/article/details/131698397