验证QCheckBox stateChanged?

时间:2016-08-10 12:24:08

标签: qt user-interface checkbox qt-creator

原谅我,因为我知道这可能是一个非常简单的问题,但我还需要另一双眼睛。我的GUI上有一个Checkbox,盒子的状态(开/关)将直接改变我的中断服务程序。这似乎非常简单,但我不能使用:

this->ui->checkBox_2->isChecked();

作为验证者,因为“无效使用”这个“非成员函数” 除此之外,我试图保存stateChanged(int arg1)的值 我可以用我的ISR调用一些指针或变量,但我想我有范围困难。 欢迎任何建议!

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
                                        ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(this->ui->pushButton_8,SIGNAL(clicked()),this,SLOT(on_pushButton_8_clicked()));
    //connect(this->ui->checkBox_2,SIGNAL(clicked(bool)),this,SLOT(myInterruptRIGHT));
    //connect(this->ui->checkBox_2,SIGNAL(clicked(bool)),this,SLOT(myInterruptLEFT));
    // connect(on_checkBox_2_stateChanged(int arg1)(),SIGNAL(clicked(bool checked)),this,SLOT(myInterruptRIGHT));
    ui->pushButton->setText("STOP");


    ui->verticalSlider->setMinimum(6);
    ui->verticalSlider->setMaximum(8);
}

void MainWindow::on_checkBox_2_stateChanged(int arg1)
{
    QCheckBox *cb2 = new QCheckBox;
    connect(cb2,SIGNAL(stateChanged(int)),this,SLOT(on_checkBox_2_stateChanged(int)));
    int sensor = digitalRead(23);
    //Does a bunch of stuff
}

void myInterruptRIGHT (void)
{
    //if(this->ui->checkBox_2->isChecked())

    if(ui->checkBox_2->isChecked())

    { //Does stuff
    }
    else
    { //more stuff
    }
}


PI_THREAD(RightTop)
{
    for(;;)
    {

        wiringPiISR(23,INT_EDGE_RISING,&myInterruptRIGHT);

    }
}

我为这些愚蠢的代码道歉,我一直在测试一堆不同的东西,没有任何证据证明非常有效。

1 个答案:

答案 0 :(得分:0)

问题1:MainWindow::on_checkBox_2_stateChanged创建复选框并将自身连接到复选框信号的插槽?确保复选框在构造函数中的某处创建,或者在UI预先设计的代码部分中创建。

问题2:PI_THREAD似乎不是Qt线程用槽来捕获信号。您仍然可以为嵌入式应用程序执行某些操作。

请注意,我当然无法测试解决方案,但我确实在实际应用中应用了类似的技术。您也可以考虑std::atomic<T>

class MainWindow : public QMainWindow
{
     public:
        QAtomicInt& atomicChkBox2State() {return m_chkBox2StateAtomic;}
     //// snip
     private:
        QAtomicInt m_chkBox2StateAtomic;
     //// snip
};


void MainWindow::on_checkBox_2_stateChanged(int state)
{
    m_chkBox2StateAtomic = state;
}


// where you create MainWindow you need to get that pointer somehow

static MainWindow* s_pMainWnd;
MainWindow* getMainWindow() {return s_pMainWnd;} // declare it in the .h file

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    s_pMainWnd = &w; // take an address

    w.show();

    return a.exec();
}

// and that ISR should read the atomic variable in case if does poll for check:

void myInterruptRIGHT (void)
{
    // now poll the atomic variable reflecting the state of checkbox
    if(getMainWindow()->atomicChkBox2State())

    { //Does stuff
    }
    else
    { //more stuff
    }
}

这是因为ISR在系统中断向量调用时不断轮询原子变量。如果中断服务程序应该从复选框中获取非常自己的终止信号,那么我们无法在不了解您的嵌入式平台“如何从'侧'而不是通过系统中断终止ISR”的情况下回答这个问题。