问题
I Have the version of Qt that is built into ubuntu 11.10. And am trying to use a QDockWidget
that cannot actually dock (basically, I just want a window that floats. I don't want to just make the view be a top level view because then I would have the OS window bar up there, which I don't want, and if I were to hide it, then the window won't be movable).
So, I basically make a new Qt Gui project, and don't change any of the files, except for the mainwindow.cpp
file, which I change to:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDockWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget *dockWidget = new QDockWidget(this);
// Without window management and attached to mainwindow (central widget)
dockWidget->setFloating( true );
// resize by frame only - not positional moveable
dockWidget->setFeatures( QDockWidget::DockWidgetMovable );
// never dock in mainwindow
dockWidget->setAllowedAreas( Qt::NoDockWidgetArea );
// title
dockWidget->setWindowTitle( "Dock Widget" );
// add contents. etc etc....
dockWidget->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
The problem is that when I go to move the widget, the whole program crashes. I want to know if I am doing something very wrong, or if there is just a bug in qt.
回答1:
You made the widget floating but not floatable.
dockWidget->setFeatures( QDockWidget::DockWidgetMovable |
QDockWidget::DockWidgetFloatable );
And you can also have a movable frameless window, by handling the mouse dragging yourself through mousePressEvent
, mouseReleaseEvent
and mouseMoveEvent
.
How to hide the now useless float button
Based on QDockWidget
source code, the "float button" is not shown if there is a title bar widget:
dockWidget->setTitleBarWidget(new QLabel("Dock Widget", dockWidget));
Or since it has a name (which isn't documented), you can hide it explicitly:
QAbstractButton * floatButton =
dockWidget->findChild<QAbstractButton*>("qt_dockwidget_floatbutton");
if(floatButton)
floatButton->hide();
来源:https://stackoverflow.com/questions/9559666/qdockwidget-causes-qt-to-crash