| #include "mainwindow.h" #include <QPainter> MainWindow :: MainWindow() { mainWindow = new QWidget; this->setCentralWidget(mainWindow); backThread = new Thread(); backThread->start(); languageCombo = new QComboBox(); languageCombo->addItem(tr("english")); languageCombo->addItem(tr("chinese")); languageCombo->setStyleSheet("font-size : 15px"); clearButton = new QPushButton(tr("clear")); clearButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); clearButton->setStyleSheet("font-size : 15px"); runStopButton = new QPushButton(tr("stop")); runStopButton->setStyleSheet("font-size : 15px"); messageEdit = new QTextEdit(); messageEdit->setReadOnly(true); messageEdit->setStyleSheet("font-size : 17px"); mainLayout = new QGridLayout; mainLayout->addWidget(messageEdit,0,0,5,5); mainLayout->addWidget(clearButton,1,5,1,1); mainLayout->addWidget(runStopButton,0,5,1,1); mainLayout->addWidget(languageCombo,2,5,1,1); mainWindow->setLayout(mainLayout); QObject::connect(clearButton,SIGNAL(clicked()),this,SLOT(clearMessage())); QObject::connect(runStopButton,SIGNAL(clicked()),this,SLOT(runStopThread())); QObject::connect(languageCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(languageChange(int))); QObject::connect(backThread,SIGNAL(OutMessage(QString)),this,SLOT(updateMessage(QString))); this->setWindowTitle(tr("thread test~")); this->setStyleSheet("font-size : 15px"); this->resize(QSize(350,135)); } MainWindow :: ~MainWindow() { mainWindow->deleteLater(); mainLayout->deleteLater(); messageEdit->deleteLater(); clearButton->deleteLater(); runStopButton->deleteLater(); } void MainWindow::updateMessage(QString message) { messageEdit->append(message); } void MainWindow::clearMessage() { messageEdit->clear(); } void MainWindow::runStopThread() { if(backThread->getState()) { backThread->stop(); runStopButton->setText(tr("run")); } else { backThread->setRun(); runStopButton->setText(tr("stop")); } } void MainWindow::languageChange(int index) { switch(index) { case 0: translator->load(QString("en.qm")); break; case 1: translator->load(QString("zh.qm")); break; } updateShow(); } void MainWindow::setTranslate(QTranslator * tran) { translator = tran; } void MainWindow::updateShow() { if(!backThread->getState()) { runStopButton->setText(tr("run")); } else { runStopButton->setText(tr("stop")); } setWindowTitle(tr("thread test~")); clearButton->setText(tr("clear")); } void MainWindow::keyPressEvent(QKeyEvent * key) { close(); } |