Qt窗口双屏幕显示及拖动

双屏幕显示

界面程序,需要启动2个实例,分别放在两个屏幕上:
实现方式:

        QDesktopWidget *desktop = QApplication::desktop();
        int screenNum = desktop->numScreens();

    1
    2

获取当前环境的屏幕个数,如果为2个屏幕,返回值为2。

根据屏幕索引号获取屏幕位置,如果为2个屏幕,默认计算机主屏幕index=0,外接显示器index = 1:

        int index=1;
        QRect rect = desktop->screenGeometry(index);

    1
    2

在显示窗口前,调用setGeometry。

        MainWindow* window = new MainWindow (NUll);
        window ->setGeometry(rect);
        window ->show();

 

另外一个实例采用相同的方式设定显示的屏幕。

注:
之前尝试使用QWidget *screen(int screen = -1);直接获取当前屏幕对于的QWidget 对象,发现不能生效。

        QDesktopWidget *desktop = QApplication::desktop();
        QWidget *parent  = desktop -> screen(1);
        MainWindow* window = new MainWindow (parent);

 

最后发现,在使用Qt-4.8版本中,windows下该接口没有实际功能。

/*qdesktopwidget_win.cpp*/
QWidget *QDesktopWidget::screen(int /* screen */)
{
    // It seems that a Qt::WType_Desktop cannot be moved?
    return this;
}

 

双屏幕间拖动

在主窗口类下,
mousePressEvent中,保存窗口原来的窗口索引old_index ;
mouseReleaseEvent获取当前的索引,如果与old_index不同,发送信号,通知切换屏幕;

切换屏幕槽函数:

void MainWindow::slotSwitchScreen(const int nScreenNo)
{
    QRect oRect = QApplication::desktop()->screenGeometry(nScreenNo);
    this->move(oRect.topLeft());
    this->showFullScreen();
}

原文:https://blog.csdn.net/weixin_39763552/article/details/80422990
 

可以通过以下步骤实现在Qt窗口拖动图片并显示: 1. 创建一个QWidget窗口,例如MainWindow。 2. 在窗口中添加一个QLabel控件,用于显示图片。可以使用setAlignment()方法来设置图片在控件中的对齐方式。 3. 为窗口设置接受拖放事件的属性:setAcceptDrops(true)。 4. 实现dragEnterEvent()方法,检查拖放事件中是否包含可接受的MIME类型,例如image/png或image/jpeg。如果是,则接受拖放事件。 5. 实现dropEvent()方法,获取拖放事件中的数据,将其转换为QImage对象,并将其设置到QLabel控件中显示。 示例代码: ```cpp #include <QtWidgets> class MainWindow : public QWidget { public: MainWindow(QWidget *parent = nullptr) : QWidget(parent) { setAcceptDrops(true); imageLabel.setAlignment(Qt::AlignCenter); imageLabel.setFixedSize(400, 400); QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(&imageLabel); setLayout(layout); } protected: void dragEnterEvent(QDragEnterEvent *event) override { if (event->mimeData()->hasFormat("image/png") || event->mimeData()->hasFormat("image/jpeg")) { event->acceptProposedAction(); } } void dropEvent(QDropEvent *event) override { const QMimeData *mimeData = event->mimeData(); if (mimeData->hasImage()) { QImage image = qvariant_cast<QImage>(mimeData->imageData()); imageLabel.setPixmap(QPixmap::fromImage(image)); } } private: QLabel imageLabel; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec(); } ``` 在这个例子中,我们创建了一个MainWindow窗口,并添加了一个QLabel控件用于显示图片。然后,我们设置窗口接受拖放事件的属性,并实现了dragEnterEvent()和dropEvent()方法。在dropEvent()方法中,我们获取拖放事件中的QImage对象,并将其设置到QLabel控件中显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值