C++/Qt : passing variables to be changed in the class

标签 c++ qt class

所以我的项目中有两种窗体:MainWindow 和 Options Form (OptForm; QWidget); 现在,我在 MainWindow 中创建(简单地拖动到一个表单)一个 QPushButton 以打开 OptForm,并传入 OptForm 可以更改的变量。

void MainWindow::openOpt() //Slot; QPushButton calls(?) it
{
    OptForm w (this->variable1,this->variable2, this);
    w.show();
}

OptForm 的构造函数是:

OptForm::OptForm(bool & variable1, bool & variable2, QWidget *parent)
    : QWidget (parent)
{
    variable1Pointer = &variable1;
    variable2Pointer = &variable2;
    ui.setupUi(this);
}

options.h 有:

class OptForm : public QWidget
{
    Q_OBJECT

public:
    OptForm(bool & variable1, bool & variable2, QWidget *parent)

    //Pointers for encrypt and verbose
    bool * variable1Pointer;
    bool * variable2Pointer;

public slots:

    void change_variable1();
    void change_variable2();

private:
    Ui::OptForm ui;
};

现在,void change_variable1();void change_variable2(); 将 bool 值更改为 truefalse .

现在,在这些函数中我有一行 this->*variable1Pointer = true;

我得到编译器错误: '((OptForm*)this)->OptForm::variable1Pointer' 不能用作成员指针,因为它是 'bool*' 类型 我怎样才能把事情做好? (已修复,谢谢)

我需要做的另一件事是让 MainWindow 知道,当 OptForm 关闭时,检查选项是否已更改。那么,我应该把这段代码放在哪里呢?在 openOpt 中,或创建一个插槽,当 OptForm 关闭时将执行(?)?那么我怎样才能向 MainWindow 发送信号呢?

提前致谢。 (我想我把事情搞砸了很多)


好的,编译器错误已修复,但是现在,当我按下该按钮时,窗口会立即出现并立即关闭:/

最佳答案

想必你真的是这个意思;

* (this->variable1Pointer) = true;

你可以缩短为:

* variable1Pointer = true;

虽然这个类的设计对我来说似乎是错误的 - 类通常不应该修改本身不是该类成员的东西。

关于C++/Qt : passing variables to be changed in the class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3433956/

相关文章:

Eclipse 中 Scientific Linux 上的 C++14

C++ : How to use SAPI with QT Creator?

c++ - 如何将 std::string 转换为 QString

C++ 固定数量的 vector 大小作为类成员

javascript - javascript中类之间共享的属性

vba - VBA 类中的 Let 和 Get 属性

c++ - 如何在 C++ 中循环语句

c++ - 使用谷歌云获取 403 禁止错误

c++ - 如何使 AddCallback() 更安全 - 对象中不同事件的函数

c++ - 如何将特殊字符作为标题文本的一部分插入到 QMenu 中?