Get physical screen size in Qt

后端 未结 2 1702
长情又很酷
长情又很酷 2020-12-28 17:51

I\'km working in Qt, i need help to get the physical size of the screen (monitor),

In Qt one can get a QDesktopWidget from QApplication, I

2条回答
  •  被撕碎了的回忆
    2020-12-28 18:10

    Here is my (quick and dirty) example. It seems to work for me and I hope it works for you. I'm assuming you can take care of main.cpp on your own. I did this on a MacBook Air 11.6" and substituted a picture of a dime for the USA icon included with OS X:

    #ifndef WINDOW_H
    #define WINDOW_H
    
    #include 
    
    class Window : public QWidget
    {
        Q_OBJECT
    
    public:
        QWidget *canvas;
        QSlider *slider;
        QPixmap pixmap;
    
    private:
        qreal zoom;
        qreal pixels;
        qreal px_width;
        qreal px_height;
        qreal mm_width;
        qreal mm_height;
    
    public:
        Window();
        void paintEvent(QPaintEvent *);
    
    public slots:
        void setZoom(int);
    };
    
    Window::Window()
    {
        QHBoxLayout *layout = new QHBoxLayout;
    
        canvas = new QWidget;
        slider = new QSlider;
        slider->setMinimum(0);
        slider->setMaximum(100);
        slider->setValue(50);
    
        layout->addWidget(canvas);
        layout->addWidget(slider);
    
        this->setLayout(layout);
    
        if(!pixmap.load(":/resources/USA.gif"))
        {
            qDebug() << "Fatal error: Unable to load image";
            exit(1);
        }
    
        QObject::connect(slider, SIGNAL(valueChanged(int)), SLOT(setZoom(int)));
    }
    
    void Window::paintEvent(QPaintEvent *event)
    {
        QPainter paint;
        paint.begin(this);
        paint.scale(zoom, zoom);
        paint.drawPixmap(0,0, pixmap);
        paint.end();
    }
    
    void Window::setZoom(int new_zoom)
    {
        zoom = (qreal)(50+new_zoom) /50;
        pixels = pixmap.width() * zoom;
    
        QDesktopWidget desk;
    
        px_width = desk.width() / pixels;
        px_height = desk.height() / pixels;
        mm_width = px_width * 17.9;
        mm_height = px_height * 17.9;
    
        qDebug() << "Zoom: " << zoom;
        qDebug() << "desk.widthMM:" << desk.widthMM();
        qDebug() << "px_width: " << px_width;
        qDebug() << "px_height: " << px_height;
        qDebug() << "mm_width: " << mm_width;
        qDebug() << "mm_height: " << mm_height;
    
        this->repaint();
    }
    
    #include "moc_window.cpp"
    
    #endif // WINDOW_H
    

提交回复
热议问题