|
| 1 | +#include "mainwindow.h" |
| 2 | +#include "ui_mainwindow.h" |
| 3 | +#include <QScreen> |
| 4 | +#include <QFileDialog> |
| 5 | +#include <QMessageBox> |
| 6 | +#include <QFile> |
| 7 | +#include <QTime> |
| 8 | + |
| 9 | +#ifdef Q_OS_WIN |
| 10 | +#include "windows.h" |
| 11 | +#pragma comment(lib, "user32.lib") |
| 12 | +#endif |
| 13 | + |
| 14 | +#ifdef Q_OS_LINUX |
| 15 | +#include <X11/Xlib.h> |
| 16 | +#endif |
| 17 | + |
| 18 | +MainWindow::MainWindow(QWidget *parent) : |
| 19 | + QMainWindow(parent), |
| 20 | + ui(new Ui::MainWindow) |
| 21 | +{ |
| 22 | + ui->setupUi(this); |
| 23 | + |
| 24 | + m_timer = new QTimer(this); |
| 25 | + connect(m_timer, SIGNAL(timeout()), this, SLOT(update())); |
| 26 | + |
| 27 | + m_nExecutionType = 0; |
| 28 | + m_nScreen_width = 0; |
| 29 | + m_nScreen_height = 0; |
| 30 | +} |
| 31 | + |
| 32 | +MainWindow::~MainWindow() |
| 33 | +{ |
| 34 | + delete ui; |
| 35 | +} |
| 36 | + |
| 37 | +void MainWindow::on_pushButton_start_clicked() |
| 38 | +{ |
| 39 | + m_timer->start(ui->spinBox_timer->value()); |
| 40 | +} |
| 41 | + |
| 42 | +void MainWindow::update() |
| 43 | +{ |
| 44 | + QString strOprType = ""; |
| 45 | + |
| 46 | + switch (m_nExecutionType) { |
| 47 | + case 0: |
| 48 | + { |
| 49 | + QScreen* screen = QGuiApplication::primaryScreen(); |
| 50 | + QRect mm = screen->availableGeometry(); |
| 51 | + m_nScreen_width = mm.width(); |
| 52 | + m_nScreen_height = mm.height(); |
| 53 | + strOprType = "Qt API"; |
| 54 | + break; |
| 55 | + } |
| 56 | + case 1: |
| 57 | + { |
| 58 | +#ifdef Q_OS_WIN |
| 59 | + m_nScreen_width = GetSystemMetrics(SM_CXSCREEN); |
| 60 | + m_nScreen_height = GetSystemMetrics(SM_CYSCREEN); |
| 61 | + strOprType = "Windows API"; |
| 62 | +#endif |
| 63 | + break; |
| 64 | + } |
| 65 | + case 2: |
| 66 | + { |
| 67 | +#ifdef Q_OS_LINUX |
| 68 | + Display *display = XOpenDisplay(NULL); |
| 69 | + int screen_num = DefaultScreen(display); |
| 70 | + m_nScreen_width = XDisplayWidth(display,screen_num); |
| 71 | + m_nScreen_height = XDisplayHeight(display,screen_num); |
| 72 | + strOprType = "Linux API"; |
| 73 | +#endif |
| 74 | + break; |
| 75 | + } |
| 76 | + default: |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + QString str = QString("[%1]-> Width: %2, Height: %3 Operation:%4") |
| 81 | + .arg(getSystemTime()) |
| 82 | + .arg(m_nScreen_width) |
| 83 | + .arg(m_nScreen_height) |
| 84 | + .arg(strOprType); |
| 85 | + |
| 86 | + |
| 87 | + ui->textEdit->append(str); |
| 88 | +} |
| 89 | + |
| 90 | +void MainWindow::on_pushButton_clear_clicked() |
| 91 | +{ |
| 92 | + ui->textEdit->clear(); |
| 93 | +} |
| 94 | + |
| 95 | +void MainWindow::on_pushButton_stop_clicked() |
| 96 | +{ |
| 97 | + m_timer->stop(); |
| 98 | +} |
| 99 | + |
| 100 | +void MainWindow::on_pushButton_Qt_clicked() |
| 101 | +{ |
| 102 | + m_nExecutionType = 0; |
| 103 | +} |
| 104 | + |
| 105 | +void MainWindow::on_pushButton_Win_clicked() |
| 106 | +{ |
| 107 | + m_nExecutionType = 1; |
| 108 | +} |
| 109 | + |
| 110 | +void MainWindow::on_pushButton_Linux_clicked() |
| 111 | +{ |
| 112 | + m_nExecutionType = 2; |
| 113 | +} |
| 114 | + |
| 115 | +void MainWindow::on_pushButton_save_clicked() |
| 116 | +{ |
| 117 | + QString filePath = QFileDialog::getSaveFileName(this, tr("Save Data"), "", |
| 118 | + tr("Data Files (*.txt)")); |
| 119 | + if (filePath.isNull()) |
| 120 | + return; |
| 121 | + |
| 122 | + QFile file(filePath); |
| 123 | + if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) |
| 124 | + { |
| 125 | + QMessageBox::information(this, QString("Hint"), |
| 126 | + QString("")); |
| 127 | + return; |
| 128 | + } |
| 129 | + QString data = ui->textEdit->toPlainText(); |
| 130 | + file.write(data.toLatin1()); |
| 131 | +} |
| 132 | + |
| 133 | +QString MainWindow::getSystemTime() |
| 134 | +{ |
| 135 | + QTime *time = new QTime(); |
| 136 | + QString strTime; |
| 137 | + strTime = time->currentTime().toString("HH:mm:ss.zzz"); |
| 138 | + return strTime; |
| 139 | +} |
| 140 | + |
| 141 | +void MainWindow::on_spinBox_timer_valueChanged(int arg) |
| 142 | +{ |
| 143 | + if(m_timer->isActive()) |
| 144 | + m_timer->start(arg); |
| 145 | +} |
0 commit comments