如何检查 QWidget 是否已经显示?
我正在使用 PyQt4 为现有应用程序开发插件 UI。该窗口是通过按下主窗口中的按钮使用 uic.loadUi() 创建的。问题是,如果我再次按下按钮(当窗口显示时),窗口将重新创建,未保存的更改将丢失。我不想让窗口成为模态窗口。我必须采取哪些选择来解决这个问题?我想这与检查 QWidget 是否已经显示有关。
I'm developing a plugin UI for an existing application using PyQt4. The window is created using uic.loadUi() on the press of a button in the main window. The problem is that if I press the button again (while the window is showing) the window is re-created and unsaved changes are lost. I don't want to make the window modal. Which options do I have to cope with this problem? I guess it would be related to checking whether the QWidget is already showing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将指向 QWidget(成员变量)的指针初始化为 0。
按下按钮时,检查指针是否为 0 - 如果是,则加载并显示该小部件,并将指针变量分配为指向新的小部件。如果按下按钮时指针不为空,则调用
widget->raise()
和widget->activateWindow()
。禁用的按钮可能会让用户感到沮丧,因为按钮的效果被隐藏了,所以看起来什么也不做。
You should initializer a pointer to the QWidget (member variable) to 0.
When the button is pressed, check if the pointer is 0 - if it is, load and show the widget, and assign the pointer variable to point to the new widget. If the pointer is not null when the button is pressed, call
widget->raise()
andwidget->activateWindow()
.Disabled buttons can be frustrating to users, as can buttons which appear to do nothing because e.g. their effect is hidden.
我本以为这将更多地由您的应用程序逻辑来处理,而不是其他任何事情。主窗口应在单击按钮后禁用该按钮,然后在窗口关闭时再次重新启用它。将辅助窗口上的关闭信号连接到主窗口上的插槽,以在辅助窗口关闭时通知主窗口。
I would have thought that this would be handled more by your application logic than anything else. The main window should disable the button after it has been clicked and then re-enable it again when the window is closed. Connect up a closing signal on the secondary window to a slot on the main window to notify the main window when the secondary window is being closed.