最近由於項目需要,采用Qt開發的程序要能夠支持雙屏顯示,在網上調研了以下,發現網頁http://www.ics.com/blog/whats-new-qt-5-qscreen-class#.Ud9mY4csnXB中對於Qt的多屏幕管理有一定論述,指出采用QScreen可以獲取窗口的相關信息,代碼如下:
#include <QGuiApplication> #include <QScreen> #include <QDebug> /* Example of using Qt 5 QScreen class. */ // Helper function to return display orientation as a string. QString Orientation(Qt::ScreenOrientation orientation) { switch (orientation) { case Qt::PrimaryOrientation : return "Primary"; case Qt::LandscapeOrientation : return "Landscape"; case Qt::PortraitOrientation : return "Portrait"; case Qt::InvertedLandscapeOrientation : return "Inverted landscape"; case Qt::InvertedPortraitOrientation : return "Inverted portrait"; default : return "Unknown"; } } int main(int argc, char *argv) { QGuiApplication a(argc, &argv); qDebug() << "Number of screens:" << QGuiApplication::screens().size(); qDebug() << "Primary screen:" << QGuiApplication::primaryScreen()->name(); foreach (QScreen *screen, QGuiApplication::screens()) { qDebug() << "Information for screen:" << screen->name(); qDebug() << " Available geometry:" << screen->availableGeometry().x() << screen->availableGeometry().y() << screen->availableGeometry().width() << "x" << screen->availableGeometry().height(); qDebug() << " Available size:" << screen->availableSize().width() << "x" << screen->availableSize().height(); qDebug() << " Available virtual geometry:" << screen->availableVirtualGeometry().x() << screen->availableVirtualGeometry().y() << screen->availableVirtualGeometry().width() << "x" << screen->availableVirtualGeometry().height(); qDebug() << " Available virtual size:" << screen->availableVirtualSize().width() << "x" << screen->availableVirtualSize().height(); qDebug() << " Depth:" << screen->depth() << "bits"; qDebug() << " Geometry:" << screen->geometry().x() << screen->geometry().y() << screen->geometry().width() << "x" << screen->geometry().height(); qDebug() << " Logical DPI:" << screen->logicalDotsPerInch(); qDebug() << " Logical DPI X:" << screen->logicalDotsPerInchX(); qDebug() << " Logical DPI Y:" << screen->logicalDotsPerInchY(); qDebug() << " Orientation:" << Orientation(screen->orientation()); qDebug() << " Physical DPI:" << screen->physicalDotsPerInch(); qDebug() << " Physical DPI X:" << screen->physicalDotsPerInchX(); qDebug() << " Physical DPI Y:" << screen->physicalDotsPerInchY(); qDebug() << " Physical size:" << screen->physicalSize().width() << "x" << screen->physicalSize().height() << "mm"; qDebug() << " Primary orientation:" << Orientation(screen->primaryOrientation()); qDebug() << " Refresh rate:" << screen->refreshRate() << "Hz"; qDebug() << " Size:" << screen->size().width() << "x" << screen->size().height(); qDebug() << " Virtual geometry:" << screen->virtualGeometry().x() << screen->virtualGeometry().y() << screen->virtualGeometry().width() << "x" << screen->virtualGeometry().height(); qDebug() << " Virtual size:" << screen->virtualSize().width() << "x" << screen->virtualSize().height(); } }
於是,有了建立基於Qt的雙屏顯示程序的思路:建立兩個QMainWindow,然后分別通過函數setGeometry分別指定兩個QMainWindow為顯示器一和顯示器二的位置,這樣總體的效果就是這個程序能夠實現雙屏的功能。同時需要注意,在定義QMainWindow的對象時,需要使用堆內存的方法,不能采用棧內存,在網頁http://www.qtcentre.org/archive/index.php/t-16439.html中對此有過論述。