#include#include int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; window.resize(250, 150); window.move(100, 100); // move to (100, 100) window.setWindowTitle("Move Example"); window.show(); return app.exec(); }
#includeIn this example, we create a custom `QWidget` that contains a `QPushButton`. We connect the `clicked` signal of the button to a slot that moves the widget down by 10 pixels when the button is clicked. This example uses the same `move()` function as example 1, but instead of moving the widget to a specific position, we move it relative to its current position using the `pos()` function and the `QPoint` class. The Qt library provides many other functions and classes for manipulating widgets and building user interfaces.#include class MyWidget : public QWidget { public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) { setFixedSize(250, 150); button = new QPushButton("Move", this); button->setGeometry(50, 50, 100, 30); connect(button, &QPushButton::clicked, this, &MyWidget::onButtonClicked); } private: QPushButton *button; void onButtonClicked() { move(pos() + QPoint(10, 10)); // move down by 10 pixels } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget window; window.setWindowTitle("Move Example"); window.show(); return app.exec(); }