How to customize windows in qt5?

In Qt5, you can customize windows by inheriting from the QWidget or QMainWindow class.

Here is an example code for a custom form.

#include <QtWidgets>

class CustomWindow : public QWidget
{
public:
    CustomWindow(QWidget *parent = nullptr) : QWidget(parent)
    {
        // 设置窗体的标题和大小
        setWindowTitle("Custom Window");
        setFixedSize(400, 300);

        // 创建和设置窗体的其他控件
        QLabel *label = new QLabel("Hello, World!", this);
        label->setFont(QFont("Arial", 20));
        label->setAlignment(Qt::AlignCenter);

        QPushButton *button = new QPushButton("Click me", this);
        connect(button, &QPushButton::clicked, this, &CustomWindow::onButtonClick);

        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(label);
        layout->addWidget(button);
        setLayout(layout);
    }

private slots:
    void onButtonClick()
    {
        QMessageBox::information(this, "Message", "Button clicked!");
    }
};

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

    CustomWindow window;
    window.show();

    return app.exec();
}

In the code example above, we have created a CustomWindow class that inherits from the QWidget class. In the constructor of CustomWindow, we set the title and size of the window, create a label and a button, and then add them to the layout of the window.

By invoking the setLayout() function, we set the layout as the primary layout of the window. Finally, we created a CustomWindow object in the main() function and displayed it.

You can further customize the controls and layout of the form according to your own needs.

bannerAds