简体   繁体   English

Qt 中的标准“关于”对话框

[英]Standard "About" dialog in Qt

What's the standard way to implement an "About" application dialog in Qt?在 Qt 中实现“关于”应用程序对话框的标准方法是什么? You know, the kind that pops up when you go Help > About... from the application menu.您知道,当您从应用程序菜单中转到“帮助”>“关于...”时弹出的那种。 I'm using Qt Designer to layout my main window, but I don't need anything fancy.我正在使用 Qt Designer 来布局我的主窗口,但我不需要任何花哨的东西。 It would be nice to do it in 2 lines of code instead of creating a new class or a new form in Qt Designer...最好用 2 行代码来完成,而不是在 Qt Designer 中创建一个新类或一个新表单......

您可以将QMessageBox::about用于简单的 about 对话框,或者如果您需要更特别/更花哨的内容,则可以编写自己的 QDialog 子类。

  1. Create a form.创建一个表单。 Right click on Project, Add New.., then select Qt in Files and Classes, select Qt Designer Form Class on right side and click choose..右键单击Project,Add New..,然后在Files and Classes中选择Qt,在右侧选择Qt Designer Form Class,然后单击选择..
  2. Select Dialog without Buttons and click next.选择没有按钮的对话框,然后单击下一步。
  3. Name it, for example "About".为其命名,例如“关于”。
  4. Open About.ui in designer and change this window as desired, ie add icon, text, buttons (maybe only OK button) and save it.在设计器中打开 About.ui 并根据需要更改此窗口,即添加图标、文本、按钮(可能只有确定按钮)并保存。
  5. In mainwindow.h add this object, ie About *about;在 mainwindow.h 中添加这个对象,即About *about;
  6. In mainwinodw.cpp instantiate it, about = new About(this);在 mainwinodw.cpp 中实例化它, about = new About(this); If you put 0 instead of this , it will not be a "modal" window, so add this in parentheses.如果你把0而不是this ,它不会是一个“模态”窗口,所以在括号中添加this
  7. Go to Designer and in Action Editor right click on menu item and select Go to slot -> triggered.转到设计器并在操作编辑器中右键单击菜单项并选择转到插槽 -> 触发。
  8. Write about->show();写关于about->show(); in that slot.在那个插槽中。

In my program Wallch ( http://sourceforge.net/projects/wall-changer/ ), i have added a new qt designer form class.在我的程序 Wallch ( http://sourceforge.net/projects/wall-changer/ ) 中,我添加了一个新的 qt 设计器表单类。

It works just fine!它工作得很好!

( I referred the name of my application so if you want to check the project , not because it is my app ) (我提到了我的应用程序的名称,所以如果您想检查该项目,而不是因为它是我的应用程序)

Here's how I did it with Python/PySide2:这是我使用 Python/PySide2 的方法:

First set up the menus/actions (I did this inside the __init__ function of my QMainWindow subclass):首先设置菜单/操作(我在QMainWindow子类的__init__函数中执行此操作):

menu = self.menuBar().addMenu('&Help')

about_action = QAction('&About', self)
about_action.triggered.connect(self.about)
menu.addAction(about_action)

Then create a new slot to call QMessageBox.about :然后创建一个新槽来调用QMessageBox.about

@Slot()
def about(self):
    QMessageBox.about(self, 'title', 'text')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM