专注于 PyQt 中的选项卡式 QDockWidget

发布于 2024-08-01 22:58:46 字数 429 浏览 5 评论 0原文

我有三个 QDockWidget,它们在启动时使用 QMainWindow.tabifyDockWidget 进行选项卡化。

在主窗口中,在所有 addDockWidget 调用之后:

self.tabifyDockWidget(self.dock_widget1, self.dock_widget2)
self.tabifyDockWidget(self.dock_widget1, self.dock_widget3)

基于某些操作,我想选择这些选项卡之一并将其置于焦点上,或者,在其他两个选项卡之上,如果它还不可见。 我尝试使用 setVisible 和 setWindowState(Qt.WindowActive) ,但没有任何变化。

有没有办法以编程方式选择选项卡式停靠小部件并将其置于前面?

I have three QDockWidgets which are tabbed at startup using QMainWindow.tabifyDockWidget.

In the main window, after all of the addDockWidget calls:

self.tabifyDockWidget(self.dock_widget1, self.dock_widget2)
self.tabifyDockWidget(self.dock_widget1, self.dock_widget3)

Based on certain actions, I'd like to select one of these tabs and bring it to focus, or, on top of the other two, if it's not already visible. I've tried using setVisible and setWindowState(Qt.WindowActive), but nothing changes.

Is there a way to programmatically select a tabbed dock widget and bring it to the front?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜扑 2024-08-08 22:58:46

感谢 qt-interest 邮件列表上的答案,使用 QWidget.raise() 可以非常简单地做到这一点:

http://qt-project.org/doc/qt-4.8/qwidget.html#raise

在 PyQt 中,它是 QWidget.raise_ ():

http://pyqt.sourceforge.net/Docs/ PyQt4/qwidget.html#raise

Thanks to an answer on the qt-interest mailing list, this is very simple to do with QWidget.raise():

http://qt-project.org/doc/qt-4.8/qwidget.html#raise

In PyQt, it's QWidget.raise_():

http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#raise

埋情葬爱 2024-08-08 22:58:46

对我来说:

dock2.show();

停靠2.raise();

就足够了。 但是,是的,两者都需要!

For me:

dock2.show();

dock2.raise();

was enough. But yes, both are needed!

阳光的暖冬 2024-08-08 22:58:46

我还没有对此进行测试,但这是我在 Qt 4.5+ 中尝试的方法(我将 PyQt 转换留给您):

class MyMainWindow ; // A QMainWindow

void MyMainWindow::bringToFront( QDockWidget* dockIn )
{
   QList<QDockWidget*> docks = tabifiedDockWidgets( dockIn ) ;
   foreach( QDockWidget* dock, docks )
   {
      // Move second dock on top of first dock widget.
      tabifyDockWidget( dock, dockIn ) ;
   }
}

请参阅 QMainWindow::tabifiedDockWidgets()QMainWindow::tabifyDockWidget()

I haven't tested this, but here's what I would try in Qt 4.5+ (I'll leave the PyQt conversion to you):

class MyMainWindow ; // A QMainWindow

void MyMainWindow::bringToFront( QDockWidget* dockIn )
{
   QList<QDockWidget*> docks = tabifiedDockWidgets( dockIn ) ;
   foreach( QDockWidget* dock, docks )
   {
      // Move second dock on top of first dock widget.
      tabifyDockWidget( dock, dockIn ) ;
   }
}

See QMainWindow::tabifiedDockWidgets() and QMainWindow::tabifyDockWidget().

零度℉ 2024-08-08 22:58:46

这对我不起作用:

dock2.raise_() 

我设法使用以下方法让它工作:

dock2.show()
parent.tabifyDockWidget(dock1, dock2)

This didn't work for me:

dock2.raise_() 

I managed to get it working using:

dock2.show()
parent.tabifyDockWidget(dock1, dock2)
灯角 2024-08-08 22:58:46

如果您正在寻找一种解决方案来将焦点设置到小部件而不是将其保留在后台的选项卡式 QDockWidget 上,您可以使用 可见性更改信号焦点代理像这样:

class Dock(QDockWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.visibilityChanged.connect(self.setVisibility)

    def setVisibility(self, visible):
        if visible: self.setFocus()

    def setWidget(self, widget:QWidget):
        super().setWidget(widget)
        self.setFocusProxy(widget)

一个小例子:

app = QApplication([])
main = QMainWindow()
dock1 = Dock('Dock1', main )
dock1.setWidget(QTextEdit(dock1))
dock2 = Dock('Dock2', main )
dock2.setWidget(QTextEdit(dock2))
main.addDockWidget(Qt.LeftDockWidgetArea, dock1)
main.tabifyDockWidget(dock1, dock2)
main.show()
app.exec()

If you are looking for a solution to set the focus to the widget and not leave it on a tabified QDockWidget in the background you can use the visibility-changed signal and a focus proxy like this:

class Dock(QDockWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.visibilityChanged.connect(self.setVisibility)

    def setVisibility(self, visible):
        if visible: self.setFocus()

    def setWidget(self, widget:QWidget):
        super().setWidget(widget)
        self.setFocusProxy(widget)

a little working example:

app = QApplication([])
main = QMainWindow()
dock1 = Dock('Dock1', main )
dock1.setWidget(QTextEdit(dock1))
dock2 = Dock('Dock2', main )
dock2.setWidget(QTextEdit(dock2))
main.addDockWidget(Qt.LeftDockWidgetArea, dock1)
main.tabifyDockWidget(dock1, dock2)
main.show()
app.exec()
笑饮青盏花 2024-08-08 22:58:46

对我有用的解决方案是:

tabifyDockWidget(dock1, dock2)
dock2.setVisible(True)
dock2.setFocus()
dock2.raise_()

这 3 个功能似乎是必要的。

A solution that is working for me is :

tabifyDockWidget(dock1, dock2)
dock2.setVisible(True)
dock2.setFocus()
dock2.raise_()

That 3 functions seem necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文