Qt measures screen resolution

Qt Getting Started Practical Tutorial (Table of Contents)

What is screen resolution

When our mobile phones, tablets, personal desktop monitors, notebook monitors, and home LCD TV monitors display images, they are all composed of pixels one by one.

Different pixels display different colors, and the entire screen will appear.

Arrangement of pixels

The pixels of the monitor are arranged one after another horizontally and vertically.

Like a collection screen, the pixel resolution is high and the points are densely packed. It is difficult for our naked eyes to see the individual pixel units on the screen.

If it is a laptop computer, if we take a screenshot of a Chinese character and enlarge it, we will find that the surrounding area of ​​the Chinese character is not smooth, but jagged:

The picture above is the result of taking a partial screenshot of the screen on my laptop and then placing it.

It can be clearly seen that Chinese characters are arranged by dots of different colors. These points are pixels.

Screen Resolution

Generally speaking, the common screen resolution aspect ratio is 16:9. This is because the perspective of the external world seen by the human eye is roughly a rectangle with such proportions. Therefore, this screen resolution is more comfortable for human eyes.

Common screen resolutions are: 1920x1080 with 1920 pixels in the horizontal direction and 1080 pixels in the vertical direction.

4K picture: 1920*2 x 1080*2 or 3840x2160.

Qt measures screen resolution

When the Qt program locates the interface position, it uses x and y coordinates to describe a position on the screen. The unit is pixels.

We can draw the mouse position on the window, and when the mouse moves, we can find the screen coordinates of each position.

At the same time, we display the window size from the beginning. In this way, as we move the mouse, we can intuitively feel the meaning of pixel position and screen resolution.

Qt program

The following program completes this function. When we execute it, the only window of the program will be displayed in full screen (press the escape key to exit full screen).

Display the screen resolution and mouse position:

It can be seen that the screen resolution of my laptop is: 1920x1080
It can be seen that the screen resolution of my laptop is: 1920x1080

It can be seen that the screen resolution of my laptop is: 1920x1080.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPainter>
#include <QColor>
#include <QString>
#include <QKeyEvent>
#include <iostream>
using namespace std;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public:
    //绘制窗口内容,包括窗口大小和鼠标位置
        void paintEvent(QPaintEvent* painter1) override;
        //鼠标移动事件,获取鼠标位置
        void mouseMoveEvent(QMouseEvent* event);
        //绘制窗口大小,宽度和高度
        void drawSize(void);
        //绘制鼠标位置,位置由鼠标移动事件获得
        void drawMouse();
        //按F12在全屏和正常模式之间切换
        void keyPressEvent(QKeyEvent* event) override;
private:
    Ui::MainWindow *ui;
    //存放鼠标移动的当前位置,在鼠标移动事件中修改该位置
        QPoint m_mousePos;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDesktopWidget>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    auto t = (QMainWindow*)this;
    t->centralWidget()->setMouseTracking(true);
    this->setMouseTracking(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

inline void MainWindow::paintEvent(QPaintEvent* painter1)
{
    drawSize();
    drawMouse();
}

void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
    m_mousePos = event->pos();
    update();//repaint
}

void MainWindow::drawMouse()
{
    QPainter painter(this);
    auto w = this->geometry().width();
    auto text = "mouse=" + std::to_string(m_mousePos.x() + 1) + "," + std::to_string(m_mousePos.y() + 1);
    painter.drawText(QPoint(10, 15), QString(text.c_str()));
}

void MainWindow::keyPressEvent(QKeyEvent* event)
{
    if (event->key() == Qt::Key_Escape)
    {
        showNormal();
    }
    update();//repaint
}

void MainWindow::drawSize(void)
{
    int left = 10;
    int row = 40;
    int row_height = 15;
    int row_index = 1;
    QPainter painter(this);
    painter.drawText(QPoint(left, row), QString(("Window width=" + std::to_string(width())).c_str()));
    painter.drawText(QPoint(left, row + row_height * row_index++), QString(("Window height=" + std::to_string(height())).c_str()));
    painter.drawText(QPoint(left, row + row_height * row_index++), QString("desktop width=") + QString::number(QApplication::desktop()->width()));
    painter.drawText(QPoint(left, row + row_height * row_index++), QString("desktop height=") + QString::number(QApplication::desktop()->height()));
}

Program source code download:

QScreenSize · CalmReason/learn-qt-with-code - Code Cloud - Open Source China (gitee.com)

Supongo que te gusta

Origin blog.csdn.net/ClamReason/article/details/133199528
Recomendado
Clasificación