问题
I have the following Qt5.11.0 code to create a qdockwidget. The nature of the widget is such that it makes sense to allow the user to interactively resize the widget, via mouse, as desired when the dockwidget is floating (the example below is contrived, but I believe illustrates the problem).
When I run this and float the dockwidget into its own top-level window, it turns out to be either very hard (Linux) or impossible (OSX) to resize the dockwidget via user interaction. On RHEL Linux 7.6, hovering the mouse over the lower right corner of the floating dockwidget produces a 'resize' cursor, however the hot-spot for such behavior is at best one or two pixels wide, making it very hard and frustrating for users to resize the floating dockwidgets. On OSX 10.13.6, I do not see any option to resize the dockwidget at all via mouse interaction.
Here is the example code:
#include <QApplication>
#include <QMainWindow>
#include <QDockWidget>
#include <QTextEdit>
#include <QTextStream>
#include <QFile>
#include <QSizeGrip>
int
main( int argc, char *argv[] ) {
QApplication app( argc, argv );
QMainWindow* mw = new QMainWindow();
mw->setCentralWidget( new QWidget() );
QDockWidget* dockWidget = new QDockWidget( "Code viewer", mw );
mw->addDockWidget( Qt::LeftDockWidgetArea, dockWidget );
QTextEdit* textEdit = new QTextEdit( dockWidget );
dockWidget->setWidget( textEdit );
QFile file( "/etc/protocols" );
QString filler;
if( ! file.open( QIODevice::ReadOnly ) ) {
exit( -1 );
} else {
QTextStream in( &file );
while( ! in.atEnd() ) {
filler += in.readLine();
}
file.close();
}
textEdit->setText( filler );
mw->show();
return app.exec();
}
I've experimented with QSizeGrip() and searched all over the net, but so far to no avail.
How do I control the hot-zone size for mouse-driven floating dockwidget resizing with Qt on Linux, and how do I enable such in the first place with Qt on OSX?
回答1:
Unfortunately, the border of a detached QDockWidget is handled by the window decorator, so you can't change it by stylesheets or adjusting the size grip. Anyway, you can change the dock widget's window flags to force the window decorator to draw regular borders.
Here is an how you can do it in your example:
#include <QApplication>
#include <QMainWindow>
#include <QDockWidget>
#include <QTextEdit>
#include <QTextStream>
#include <QFile>
#include <QSizeGrip>
int main( int argc, char *argv[] ) {
QApplication app( argc, argv );
QMainWindow* mw = new QMainWindow();
mw->setCentralWidget( new QWidget() );
QDockWidget* dockWidget = new QDockWidget( "Code viewer", mw );
mw->addDockWidget( Qt::LeftDockWidgetArea, dockWidget );
// handle floating changes
QObject::connect(dockWidget, &QDockWidget::topLevelChanged, [dockWidget] (bool floating)
{
if (floating)
{
dockWidget->setWindowFlags(Qt::Window);
dockWidget->show();
}
});
QTextEdit* textEdit = new QTextEdit( dockWidget );
dockWidget->setWidget( textEdit );
QFile file( "/etc/protocols" );
QString filler;
if( ! file.open( QIODevice::ReadOnly ) ) {
exit( -1 );
} else {
QTextStream in( &file );
while( ! in.atEnd() ) {
filler += in.readLine();
}
file.close();
}
textEdit->setText( filler );
mw->show();
return app.exec();
}
Here you can find a description of all the available window flags to customize the look of your widget.
来源:https://stackoverflow.com/questions/53642531/how-to-allow-interactive-resizing-of-floating-qt5-dock-widget