55 lines
1.3 KiB
C++
55 lines
1.3 KiB
C++
#include <QApplication>
|
|
#include <QWidget>
|
|
#include <QMouseEvent>
|
|
#include <QVBoxLayout>
|
|
#include <QLabel>
|
|
#include <QTimer>
|
|
|
|
class ClickableWindow : public QWidget
|
|
{
|
|
public:
|
|
ClickableWindow(QWidget *parent = nullptr) : QWidget(parent)
|
|
{
|
|
// Create a label to show instructions
|
|
QLabel *label = new QLabel("Click anywhere to change background color!");
|
|
label->setAlignment(Qt::AlignCenter);
|
|
|
|
// Set up layout
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
layout->addWidget(label);
|
|
|
|
// Set initial background color
|
|
setStyleSheet("background-color: black;");
|
|
|
|
// Set window properties
|
|
setWindowTitle("Click to Change Background Color");
|
|
resize(400, 300);
|
|
}
|
|
|
|
protected:
|
|
void mousePressEvent(QMouseEvent *event) override
|
|
{
|
|
if (event->button() == Qt::LeftButton) {
|
|
setStyleSheet("background-color: white");
|
|
}
|
|
|
|
QTimer::singleShot(200, this, &ClickableWindow::backToBlack);
|
|
|
|
QWidget::mousePressEvent(event);
|
|
}
|
|
|
|
void backToBlack()
|
|
{
|
|
setStyleSheet("background-color: black");
|
|
}
|
|
};
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QApplication app(argc, argv);
|
|
|
|
ClickableWindow window;
|
|
window.show();
|
|
|
|
return app.exec();
|
|
} |