#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <QApplication>#include <QFont>#include <QPushButton>#include <QWidget>#include <QStyle>#include <QToolButton>#include <QMessageBox>#include <QVBoxLayout>QWidget *window;QPushButton *button2;void *ColorButton(void *threadid){ button2 = new QPushButton("Button2", window); button2->setGeometry(10, 10, 180, 40); button2->setStyleSheet(" background-color: green ;border-radius: 8px; border-color: black; border-style: outset; border-width: 2px"); button2->show();}int main(int argc, char *argv[]){ pthread_t thread1; QApplication app(argc, argv); window = new QWidget; window->resize(200, 120); QPushButton button1("Button1", window); button1.setGeometry(10, 10, 180, 40); window->show(); pthread_create(&thread1, NULL, ColorButton, NULL); sleep(1); return app.exec();}
#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <QApplication>#include <QPushButton>#include <QWidget>#include <QStyle>#include <QVBoxLayout>#include <QMetaObject>#include <QLabel>QVBoxLayout *vb;QLabel *lab;QPushButton *button1;QPushButton *button2;QPushButton *button3;void * ColorButton12(void * arg){ QMetaObject::invokeMethod(button1, "setStyleSheet" ,Q_ARG(QString, " background-color: green ;border-radius: 8px; border-color: black; border-style: outset; border-width: 2px")); QMetaObject::invokeMethod(button2, "setStyleSheet" ,Q_ARG(QString, " background-color: yellow ;border-radius: 8px; border-color: black; border-style: outset; border-width: 2px"));}void * ColorButton23(void * arg){ QMetaObject::invokeMethod(button2, "setStyleSheet" ,Q_ARG(QString, " background-color: red ;border-radius: 8px; border-color: black; border-style: outset; border-width: 2px")); QMetaObject::invokeMethod(button3, "setStyleSheet" ,Q_ARG(QString, " background-color: gray ;border-radius: 8px; border-color: black; border-style: outset; border-width: 2px"));}int main(int argc, char *argv[]){ pthread_t thread1, thread2; QApplication app(argc, argv); QWidget *window = new QWidget; vb = new QVBoxLayout(window); window->resize(200, 120); lab = new QLabel; lab->setText("label1"); button1 = new QPushButton("Button1", window); button2 = new QPushButton("Button2", window); button3 = new QPushButton("Button3", window); vb->addWidget(button1); vb->addWidget(button2); vb->addWidget(button3); vb->addWidget(lab); window->setLayout(vb); window->show(); pthread_create(&thread1, NULL, ColorButton12, NULL); pthread_join(thread1, NULL); pthread_create(&thread2, NULL, ColorButton23, NULL); sleep(1); return app.exec();}