#include#include int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget window; window.resize(250, 150); window.setWindowTitle("My Window"); window.show(); return a.exec(); }
#include#include #include class MyWindow : public QWidget { public: MyWindow(QWidget *parent = nullptr) : QWidget(parent) { resize(250, 150); setWindowTitle("My Window"); } protected: void mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) qDebug("Left mouse button pressed"); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MyWindow window; window.show(); return a.exec(); }
#includeIn this example, we create a horizontal layout for the window and add two buttons to it. We use pointers and dynamic memory allocation to manage the layout and its widgets. The QWidget class is part of the Qt framework library, which is a cross-platform toolkit for creating graphical user interfaces (GUI) in C++. It provides several useful classes for managing windows, widgets, graphics, events, etc.#include #include #include int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget window; QHBoxLayout *layout = new QHBoxLayout(&window); QPushButton *button1 = new QPushButton("Button 1"); QPushButton *button2 = new QPushButton("Button 2"); layout->addWidget(button1); layout->addWidget(button2); window.show(); return a.exec(); }