简体   繁体   English

如何在Qt应用程序中同时隐藏/显示QLabel和QTextEdit?

[英]How to hide/show a QLabel and QTextEdit at the same time in my Qt application?

I am working on a hide/show feature for my console in my Qt GUI application. 我正在Qt GUI应用程序中为控制台使用隐藏/显示功能。 The console consists of 2 widgets; 控制台包含2个小部件。 QLabel and QTextEdit. QLabel和QTextEdit。 Do I need to add the QLabel and QTextEdit to a QWidget in order to show/hide them, or is there a better way? 是否需要将QLabel和QTextEdit添加到QWidget才能显示/隐藏它们,还是有更好的方法?

So basically I am looking for a container such as 'JPanel' in Java... 所以基本上我正在寻找Java中的容器,例如“ JPanel” ...

Do I need to add the QLabel and QTextEdit to a QWidget in order to show/hide them, or is there a better way? 是否需要将QLabel和QTextEdit添加到QWidget才能显示/隐藏它们,还是有更好的方法?

Multiple methods are possible here. 这里有多种方法。 You can, as you suggest, create a parent QWidget and add the QLabel and QTextEdit to a QWidget. 您可以按照建议创建父QWidget,并将QLabel和QTextEdit添加到QWidget。 Calling show and hide on the parent widget will affect its children. 在父窗口小部件上调用显示和隐藏将影响其子窗口。

Another method would be to have a slot function, which when an action is called, the slot calls show / hide on the 2 widgets. 另一种方法是具有一个插槽功能,当调用一个动作时,插槽调用在2个小部件上显示/隐藏。

Neither is right or wrong and depends upon the overall design of your application. 对与错都取决于应用程序的整体设计。

Ok, I think you need to use a layout (horizontal / vertical): 好的,我认为您需要使用布局 (水平/垂直):

The simplest way to arrange objects on a form is to place them in a horizontal or vertical layout. 在表格上排列对象的最简单方法是将它们放置在水平或垂直布局中。 Horizontal layouts ensure that the widgets within are aligned horizontally; 水平布局可确保其中的小部件水平对齐; vertical layouts ensure that they are aligned vertically. 垂直布局可确保它们垂直对齐。

Horizontal and vertical layouts can be combined and nested to any depth. 水平和垂直布局可以组合并嵌套到任何深度。 However, if you need more control over the placement of objects, consider using the grid layout. 但是,如果您需要对对象放置的更多控制,请考虑使用网格布局。

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

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