简体   繁体   English

如何使用 ZE8801102A40AD2859DDFCFDCAEBF008 从一个 window 到另一个窗口的 class 中获取 object 的值?

[英]How do I get the value of an object from one window into another window's class with Qt Creator?

This seems like it should be a simple question.这似乎应该是一个简单的问题。 I have two QSpinBoxes in my MainWindow, with a push button beside them.我的 MainWindow 中有两个 QSpinBox,旁边有一个按钮。 The user first selects the dimensions of an array of checkboxes using these spin boxes, then clicks the push button.用户首先使用这些旋转框选择复选框数组的尺寸,然后单击按钮。 This pops up a new window with the array of checkboxes in it.这会弹出一个新的 window ,其中包含复选框数组。 The problem I am having though is that when I try to get the value of the spinboxes in my popup window's code, I get a compiler error because these buttons are private.我遇到的问题是,当我尝试在弹出窗口的代码中获取旋转框的值时,我收到编译器错误,因为这些按钮是私有的。 Here is the code:这是代码:

void DomainGeneration::createBoxes()
{
    int x_dim = MainWindow::ui->xDim->value();
    int y_dim = MainWindow::ui->yDim->value();
......the code......
}

Compiler errors:编译器错误:

'Ui::MainWindow* MainWindow::ui' is private within this context 'Ui::MainWindow* MainWindow::ui' 在此上下文中是私有的

and

object missing in reference to 'MainWindow::ui' from this location object 在此位置缺少对“MainWindow::ui”的引用

So my question is, how do I access these objects from a different window?所以我的问题是,如何从不同的 window 访问这些对象?

You have two problems:你有两个问题:

  1. MainWindow::ui is private MainWindow::ui是私有的
  2. MainWindow::ui is not static, you need an actual instance of a MainWindow to reach it MainWindow::ui不是 static,您需要 MainWindow 的实际实例才能访问它

To solve one, you usually create accessor methods in the MainWindow (or whatever class it is that needs to export some of its state).为了解决一个问题,您通常在MainWindow中创建访问器方法(或者任何需要导出其状态的 class )。

To solve two, you need a pointer to your MainWindow instance to call these accessors on.要解决两个问题,您需要一个指向MainWindow实例的指针来调用这些访问器。

In your MainWindow class, define something like:在您的 MainWindow class 中,定义如下内容:

int getXDim() const { return ui->xDim->value(); }

And to get the pointer to your main window, either pass it in to your DomainGeneration's constructor, or into that createBoxes() method, depending on how/where those are called and whether or not you'll need that pointer elsewhere in that class.要获取指向主 window 的指针,请将其传递给 DomainGeneration 的构造函数或createBoxes()方法,具体取决于调用它们的方式/位置以及在 class 的其他地方是否需要该指针。

Something like:就像是:

void DomainGeneration::createBoxes(MainWindow const* main)
{
  int x_dim = main->getXDim();
  ...
}

(Or just pass the dimensions to that methods, obviously.) (或者只是将维度传递给该方法,显然。)

(None of this is Qt specific. This is plain C++.) (这些都不是 Qt 特定的。这是普通的 C++。)

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

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