我有一个主窗口和一个单击该按钮从该窗口打开的对话框。出于性能原因,有一个对话框高速缓存,它保留对话框的一个实例,仅在应打开对话框而不是创建新实例时才显示。在对话框中,有一个QListWidget
,其中包含一些可以通过拖放更改顺序的项目。当我第一次打开对话框时,此方法有效,但是当我关闭对话框并再次打开它时,无法删除项目,我得到一个Qt::ForbiddenCursor
。
该问题似乎是由于在关闭对话框时调用setParent(nullptr)
引起的(或者可能只是通过更改父级而引起)。如果删除此行,则可以进行拖放。但是,我需要这样做以防止对话框被父级删除,并且对话框在不同的上下文中可以具有不同的父级(这在我的简化示例中并不明显)。知道这种方法有什么问题吗?我的Qt版本是5.9.3。这可以是Qt错误吗?
MainWindow.h:
#include "ui_mainwindow.h"
#include "dialog.h"
#include <QPushButton>
#include <QMainWindow>
#include <memory>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
dialog.reset(new Dialog(this));
dialog->setAttribute(Qt::WA_DeleteOnClose, false);
connect(ui->button, &QPushButton::pressed, [&]
{
dialog->setParent(this, dialog->windowFlags());
dialog->open();
});
}
~MainWindow()
{
delete ui;
}
private:
Ui::MainWindow* ui;
std::unique_ptr<Dialog> dialog;
};
Dialog.h:
#include "ui_dialog.h"
#include <QDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget* parent) : QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->listWidget->addItem("first");
ui->listWidget->addItem("second");
ui->listWidget->addItem("third");
}
~Dialog()
{
delete ui;
}
public slots:
virtual void reject() override
{
setParent(nullptr);
QDialog::reject();
}
private:
Ui::Dialog* ui;
};
Dialog.ui-具有QListWidget和拒绝按钮的简单对话框
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>548</width>
<height>397</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="listWidget">
<property name="dragDropMode">
<enum>QAbstractItemView::DragDrop</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
MainWindow.ui-具有一个按钮的默认主窗口
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>432</width>
<height>316</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="button">
<property name="geometry">
<rect>
<x>40</x>
<y>30</y>
<width>80</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>432</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
答案 0 :(得分:2)
以下再现了问题。这确实是一个Qt错误。 OP报告了该错误:https://bugreports.qt.io/browse/QTBUG-70240
问题是因为QWidget
标志处于关闭状态时Qt::Window
会重新创建放置站点,从而调用QWindowsWindow::updateDropSite
,这会做错事并调用setDropSiteEnabled(false)
。
两个等效的解决方法是:
dialog->setParent(newParent)
替换为:
auto flags = dialog->windowFlags();
dialog->setParent(newParent, {});
dialog->setWindowFlags(flags);
dialog->setParent(nullptr)
替换为:
dialog->setParent(nullptr, dialog->windowFlags());
第一个解决方法撤消了小部件的损坏状态。不需要使用第二种解决方法,即,必须始终使用该解决方法,否则必须调用一次第一种解决方法才能恢复可用的放置目标状态。
// https://github.com/KubaO/stackoverflown/tree/master/questions/dialog-parent-dnd-52061919
#include <QtWidgets>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget ui;
QVBoxLayout layout{&ui};
QPushButton button{"Toggle List"};
QCheckBox workaround1{"Workaround 1"};
QCheckBox workaround2{"Workaround 2"};
for (auto w : QWidgetList{&button, &workaround1, &workaround2}) layout.addWidget(w);
workaround2.setChecked(true);
QListWidget listWidget;
Q_ASSERT(!listWidget.testAttribute(Qt::WA_DeleteOnClose));
listWidget.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
listWidget.setDragDropMode(QAbstractItemView::DragDrop);
listWidget.setDefaultDropAction(Qt::MoveAction);
for (auto s : QStringList{"first", "second", "third"}) listWidget.addItem(s);
QObject::connect(&button, &QPushButton::pressed, [&] {
if (!listWidget.parent()) {
if (!workaround1.isChecked())
listWidget.setParent(&button, listWidget.windowFlags());
else {
auto flags = listWidget.windowFlags();
listWidget.setParent(&button, {});
listWidget.setWindowFlags(flags);
}
listWidget.show();
} else {
if (!workaround2.isChecked())
listWidget.setParent(nullptr);
else
listWidget.setParent(nullptr, listWidget.windowFlags());
listWidget.close();
}
});
ui.setMinimumSize(320, 200);
ui.show();
return app.exec();
}