Verifying QCheckBox stateChanged?

Verifying QCheckBox stateChanged?

本文关键字:stateChanged QCheckBox Verifying      更新时间:2023-10-16

原谅我,因为我知道这可能是一个非常简单的问题,但我需要另一个人的眼睛。我的GUI上有一个复选框,复选框的状态(开/关)将直接改变我的中断服务例程。这看起来超级简单,但是我不能使用:

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

作为验证器,因为"this ' is非成员函数的无效使用"除此之外,我还尝试保存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: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"之前,我们无法回答这个问题。