PDA

View Full Version : Drag'n'Drop does not work after havin called QMainWindow::showFullScreen()



Kampfgnom
25th June 2012, 18:15
Hi everyone,

it seems as if drops are not being accepted by qmainwindows (or their child widgets), that have called showFullscreen() once.
Calling setAcceptDrops(true) after the showFullscreen() call does not help.
Other MainWindows do still accept the drops, which have been dragged from the misfunctioning MainWindow.

I tried to code a minimal example which shows the problem:

mainwindow.cpp:


#include "mainwindow.h"

#include <QDragEnterEvent>
#include <QDropEvent>
#include <QLabel>
#include <QVBoxLayout>
#include <QPushButton>

class DragDropWidget : public QWidget {
public:
DragDropWidget(const QString &text, QWidget *parent);
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
void dragLeaveEvent(QDragLeaveEvent *);
void mousePressEvent(QMouseEvent *event);

QLabel *label;
};

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget *central = new QWidget(this);
QVBoxLayout *l = new QVBoxLayout(central);

widget1 = new DragDropWidget("1", this);
widget2 = new DragDropWidget("2", this);

l->addWidget(widget1);
l->addWidget(widget2);

QPushButton *fullscreen = new QPushButton(this);
connect(fullscreen, SIGNAL(clicked()), this, SLOT(onPushButtonClicked()));
l->addWidget(fullscreen);

central->setLayout(l);
setCentralWidget(central);
}

void MainWindow::onPushButtonClicked()
{
if(isFullScreen()) {
showNormal();
widget1->setAcceptDrops(true);
}
else {
showFullScreen();
widget1->setAcceptDrops(true);
}
}

DragDropWidget::DragDropWidget(const QString &text, QWidget *parent) :
QWidget(parent)
{
setStyleSheet("QWidget { border: 1px solid blue; }");
QVBoxLayout *l = new QVBoxLayout(this);
setLayout(l);
label = new QLabel(text, this);
l->addWidget(label);
setAcceptDrops(true);
}

void DragDropWidget::dragEnterEvent(QDragEnterEvent *event)
{
if(event->mimeData()->hasFormat("application/mimeType")) {
setStyleSheet("QWidget { border: 1px solid red; }");
event->acceptProposedAction();
}
}

void DragDropWidget::dropEvent(QDropEvent *event)
{
QByteArray encodedData = event->mimeData()->data("application/mimeType");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QString data;
stream >> data;
label->setText(data);
event->accept();
setStyleSheet("QWidget { border: 1px solid blue; }");
}

void DragDropWidget::dragLeaveEvent(QDragLeaveEvent *)
{
setStyleSheet("QWidget { border: 1px solid blue; }");
}

void DragDropWidget::mousePressEvent(QMouseEvent *)
{
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;

QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
stream << label->text();

mimeData->setData("application/mimeType", encodedData);
drag->setMimeData(mimeData);
drag->exec();
}


mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class DragDropWidget;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);

private slots:
void onPushButtonClicked();

private:
DragDropWidget *widget1;
DragDropWidget *widget2;
};
#endif // MAINWINDOW_H


main.cpp

#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

MainWindow w2;
w2.show();

return a.exec();
}



You can download the full project here: https://dl.dropbox.com/u/140012/dragndrop_fullscreen.zip

Do I miss something here? I cant be the first person, who has this problem.

Thank you.

wysota
25th June 2012, 19:47
Your test project works fine for me (Linux, KDE, Qt 4.8.1).

Kampfgnom
25th June 2012, 20:14
I am on Mac OS X 10.7.4 and Qt 4.8.1.

And I found a workaround to the problem. You have to set acceptDrops to true on the QMainWindow instead its child widget, after calling showFullScreen:



void MainWindow::onPushButtonClicked()
{
if(isFullScreen()) {
showNormal();
}
else {
showFullScreen();
setAcceptDrops(true);
}
}

Is this wanted behaviour? If so: Why?

I would love to test this against a current Qt5 build. But the latest clone from git does not build on my system.