QDockWidget拆分器在QMainWindow调整大小时跳转 [英] QDockWidget splitter jumps when QMainWindow Resized

查看:217
本文介绍了QDockWidget拆分器在QMainWindow调整大小时跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的旧程序中,我在QDockWidget内有2列QTableWidget.

In an old program of mine, I have 2-column QTableWidget inside of a QDockWidget.

在近代(Qt 5.10)中构建它,我似乎必须覆盖表的sizeHint()才能获取它设置为除100px以外的宽度.

Building it in modern times (Qt 5.10) I seem to have to override the table's sizeHint() to get it to be a width besides 100px or something. Which was a bit annoying (didn't need this before--why isn't the table's width via headers the default?)

但是,在进行了调整之后,又有了另一个麻烦:如果用户为停靠小部件进行了拆分器的任何移动,则在任何调整窗口大小的情况下,拆分器都会丢失.我做了一点MCVE来显示效果...这是启动情况:

But with that tweaked, there's now another nuisance: if the user does any moving of the splitter for the dock widget, it will be lost on any window resize. I made a little MCVE to show the effect...here's the startup situation:

然后让我们说用户稍微拖动分离器(可能是扩展,可能是收缩-没关系):

Then let's say the user drags the splitter a bit (could be expansion, could be contraction--doesn't matter):

现在他们将窗口的底部边缘向下拖动一点,然后 snap ,它会跳回所需的大小(突然的跳动发生在鼠标第一次开始拖动时):

Now they drag the bottom edge of the window down a bit, and snap, it jumps back to the preferred size (the sudden jump occurs right when the mouse first starts dragging):

将水平大小策略从QSizePolicy::Preferred更改为QSizePolicy::Ignored似乎不会影响此行为,不调用setStretchLastSection()似乎也无济于事.

Changing the horizontal size policy from QSizePolicy::Preferred to QSizePolicy::Ignored doesn't seem to influence this behavior, not calling setStretchLastSection() doesn't seem to help either.

我希望如果垂直调整窗口的大小以使水平分割器完全不动...并且如果水平调整窗口的大小则可以进行比跳跃更渐进的操作.有任何想法吗? Qt Creator的监视列表似乎并不存在此问题,但我对消息来源不甚了解,无法深入研究其原因.

I'd prefer if the window be resized vertically that a horizontal splitter not move at all...and if it's resized horizontally that something more gradual be done than a jump. Any ideas? Qt Creator doesn't seem to have this problem with its watchlist, but I'm not familiar enough with the source to go digging into why.

mainwindow.h:

#include <QMainWindow>

QT_BEGIN_NAMESPACE
class QTextEdit;
class SizedTableWidget;
QT_END_NAMESPACE

class SizedTableWidget;

class MainWindow : public QMainWindow {
    Q_OBJECT

    QTextEdit *textEdit;
    SizedTableWidget *table;

public:
    MainWindow ();
};

mainwindow.cpp:

#include <QtWidgets>

#include "mainwindow.h"

class SizedTableWidget : public QTableWidget {
public:
    SizedTableWidget (int rows, int columns, QWidget *parent) :
        QTableWidget (rows, columns, parent) {}

    QSize sizeHint() const {
        return QSize (
            verticalHeader()->width()
                + horizontalHeader()->length()
                + frameWidth() * 2,
            horizontalHeader()->height()
                + verticalHeader()->length()
                + frameWidth() * 2
        );
    }
};

MainWindow::MainWindow() : textEdit(new QTextEdit) {
    setCentralWidget(textEdit);

    QDockWidget *dock = new QDockWidget(tr("Table"), this);
    dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

    table = new SizedTableWidget (1 /* rows */, 2 /* cols */, dock);
    table->setHorizontalHeaderLabels(QStringList() << "name" << "value");

    QTableWidgetItem *nameItem = new QTableWidgetItem;
    nameItem->setText("foo");
    table->setItem(0, 0, nameItem);

    QTableWidgetItem *valueItem = new QTableWidgetItem;
    valueItem->setText("10");
    table->setItem(0, 1, valueItem);

    table->horizontalHeader()->setStretchLastSection(true);
    table->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);

    dock->setWidget(table);
    addDockWidget(Qt::RightDockWidgetArea, dock);
}

P.S.如果有人知道为什么Windows上的列标题和行标题没有样式,我想知道...这是故意的吗?在Linux上看起来还不错.

P.S. if anyone knows why the column and row headers on Windows don't have any styling on them, I'd be interested to know...is this on purpose? Looks fine on Linux.

系统信息:

  • Qt 5.10.0
  • Qt Creator 4.5.0
  • Windows 10 Home,1709 (内部版本16299.125)和MSVC 2017
  • Kubuntu Linux 17.10和gcc 7.2.0
  • Qt 5.10.0
  • Qt Creator 4.5.0
  • Windows 10 Home, 1709 (Build 16299.125) and MSVC 2017
  • Kubuntu Linux 17.10 and gcc 7.2.0

推荐答案

这似乎是一个错误-已被多方确认-已出现在Qt 5.10中:

This appears to be a bug--now confirmed by multiple parties--that appeared in Qt 5.10:

https://bugreports.qt.io/browse/QTBUG-65592

在注释中给出了一个建议的解决方法,直到它被修复为止.

A proposed workaround until it is fixed is given in the comments:

调用resizeDocks({nameOfAnyDock}, {40}, Qt::Horizontal)启用了调整大小功能(确切的数字无关紧要).调用addDockWidget后立即调用该函数.

Calling resizeDocks({nameOfAnyDock}, {40}, Qt::Horizontal) enabled the resize to work (the exact number is irrelevant). The function is called immediately after calling addDockWidget.

这篇关于QDockWidget拆分器在QMainWindow调整大小时跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆