c# - 将现有的 QWidget 停靠在 QWindow 中

标签 c# c++ qt c++11

对于非常熟悉 C# 或 VB.NET 使用 .NET Framework(我认为是 HitTest 门的框架)中的 UserControl 组件的人,您习惯于添加几个按钮预览不同的用户控件如下:

1) 首先,您要准备一个合适的用户界面(包含 3 个按钮和右侧区域的一个面板,用于在单击其中一个添加的按钮后查看每个用户控件)。

2) 从解决方案资源管理器中添加 3 个用户控件...

3) 在每个用户控件上插入内容...

4) 为 frmMain.cs 上的 3 个按钮实现代码如下(对于此实现,我们将实现对象名称为 welcomeBtn,其余代码将具有相同的代码,但用户控件名称不同):

private void welcomeBtn_Click(object sender, EventArgs e)
{
    //Clear up everything from the panel if any item exist(s)...
    mainPanel.Controls.Clear();

    //Create a new instance of a user control for the button...
    UserControl1_Welcome welcome = new UserControl1_Welcome();

    //Show up the created instance of the user control
    mainPanel.Controls.Add(welcome);
}

5) 最后,程序运行时最初会这样结束:

http://i.stack.imgur.com/OENwG.png

** 程序的使用 **

例如,当您点击“欢迎”按钮时,预期结果应该是这样的:

http://i.stack.imgur.com/iCyo3.png

...当您点击不同的按钮时,比方说“许可协议(protocol)”按钮,您会希望看到当前选择以外的内容。

主要问题 我们如何通过应用“QDockWidget”在QT CREATOR中带来Windows Forms的简单性?

我已经尝试插入 QDockWidget 组件没有问题,但是当我尝试执行等效的 .NET 代码以在 QDockWidget 中添加 QWidget 时:

ui->dockWidget->setWidget(myWidget);

我认为这相当于 C#.NET 中的这行代码(如果我在这里错了请纠正我):

ui.Controls.Add(myWidget);

使用这段代码后,我的程序不会崩溃,也不会显示任何正在运行的东西......

附言很抱歉链接了图片,我没有 10 个让它们出现的声誉...

最佳答案

What I want is to have a program that does the same thing with the C# example (showing a user control based on the click of a button).

如果您想根据按钮点击显示特定的小部件,我建议使用 QStackedWidget 一个简单的例子是这样的:

// In the constructor of your CustomWidget

// Create your buttons
QPushButton* firstButton = new QPushButton("First Button", this);
QPushButton* secondButton = new QPushButton("Second Button", this);
QPushButton* thirdButton = new QPushButton("Third Button", this);

// Create your (custom) widgets
QLabel* firstPageWidget = new QLabel("First Label", this);
QLabel* secondPageWidget = new QLabel("Second Label", this);
QLabel* thirdPageWidget = new QLabel("Third Label", this);

// Add them to the stackWidget
/*QStackedWidget* */ m_stackedWidget = new QStackedWidget(this);
m_stackedWidget->addWidget(firstPageWidget);
m_stackedWidget->addWidget(secondPageWidget);
m_stackedWidget->addWidget(thirdPageWidget);

// Insert buttons and stackWidget to CustomWidget 

QVBoxLayout* layoutStack = new QVBoxLayout();
layoutStack->addWidget(m_stackedWidget);

QVBoxLayout* layoutButtons = new QVBoxLayout();
layoutButtons->addWidget(firstButton);
layoutButtons->addWidget(secondButton);
layoutButtons->addWidget(thirdButton);

QHBoxLayout* layout = new QHBoxLayout();
layout->addLayout(layoutButtons);
layout->addLayout(layoutStack);
setLayout(layout);

// Connect button clicks to slots
connect(firstButton, SIGNAL(clicked()), this, SLOT(onFirstButtonClicked()));
connect(secondButton, SIGNAL(clicked()), this, SLOT(onSecondButtonClicked()));
connect(thirdButton, SIGNAL(clicked()), this, SLOT(onThirdButtonClicked()));

然后您更改插槽中的当前可见的小部件:

void CustomWidget::onFirstButtonClicked() {
    m_stackedWidget->setCurrentIndex(0);
}
void CustomWidget::onSecondButtonClicked() {
    m_stackedWidget->setCurrentIndex(1);
}
void CustomWidget::onThirdButtonClicked() {
    m_stackedWidget->setCurrentIndex(2);
}

请注意,如果您希望单击按钮只是为了简单地更改一些文本(而不是更改可见的小部件),您可能最好使用 QTextEdit而不是 QStackedWidget,并在插槽中调用 setText("...");

如果你有很多按钮,你最好使用QSignalMapper限制槽的数量。

另外,我不明白你为什么提到 QDockWidget因为它们有非常具体的用法:

The QDockWidget class provides a widget that can be docked inside a QMainWindow or floated as a top-level window on the desktop.

QDockWidget provides the concept of dock widgets, also know as tool palettes or utility windows. Dock windows are secondary windows placed in the dock widget area around the central widget in a QMainWindow.

如果您只是想要一个单独的窗口,您可能正在寻找 QDialog

如何使用 QtDesigner 执行此操作:

  1. 首先,您要准备一个合适的用户界面(包含 3 个按钮和一个位于右侧区域的 QStackedWidget,以便在单击其中一个添加的按钮后查看每个用户控件) .
  2. 为堆栈中的用户控件添加 3 个页面(如果您确实需要,则为“空”页面添加一个)。如果您想在单独的 UI 文件中设计控件/仅在代码中(而不是 MainFrame 中的所有控件),您可以添加纯 QWidgetpromote将它们分配给适当的特定小部件类型
  3. 在每个用户控件上插入内容...
  4. frmMain.cpp/.h 上的 3 个按钮的实现代码如下(对于此实现,我们将实现对象名称为 welcomeBtn 的“欢迎”按钮,其余代码相同但不同的用户控件名称):

    void FrmMain::on_welcomeBtn_clicked() { ui->stack->setCurrentWidget(ui->welcomeWidget); }

  5. 在设计器中选择“空”页面作为当前页面,这样程序运行时最初会这样结束:(你的截图)

  6. 例如,当您点击“欢迎”按钮时,结果应该是这样的:(您的第二个屏幕截图)

关于c# - 将现有的 QWidget 停靠在 QWindow 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31214646/

相关文章:

c# - 将纪元/unix 转换为日期时间

c++ - 在 visual studio 中使用 QChart

c++ - 无法在 Qwidget 中显示 OpenGL

c# - 如何将字典中的值添加到电子表格?

c# - 我如何在 C# 中反序列化 python pickles?

c# - 从 Azure 函数调用 Asp.Net Web API 端点

c++ - boost .MultiIndex : How to make an effective set intersection?

java - OpenGL加载和渲染png到屏幕

c++ - 来自 glClear() 的 OpenGL 帧缓冲区错误

qt - 如何仅设置 `Widget` 大小?