【问题标题】:Verifying QCheckBox stateChanged?验证 QCheckBox stateChanged?
【发布时间】:2016-08-10 12:24:08
【问题描述】:

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

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

作为验证器,因为“无效使用“this”是非成员函数” 除此之外,我试图只保存 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);

    }
}

对于草率的代码,我深表歉意,我已经测试了很多不同的东西,但都没有证明是非常有效的。

【问题讨论】:

    标签: c++ qt qt5


    【解决方案1】:

    问题 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”,我们将无法回答这个问题。

    【讨论】:

    • 好的,所以我试过了,它可能正在路上,但我得到了 2 个类似的错误:“没有参数列表的 QAtomicInteger 的无效使用”。我现在想,我只是用你扔在那里的东西跑,一旦我得到一些结果,也许会改变它。
    • 使用 QAtomicInt 而不是 QAtomicInteger。
    • 我认为问题可能来自我对 getMainWindow(); 的声明(或没有声明);在头文件中,因为当我尝试在 ISR 中调用它时,它无法识别。有什么我不知道的方式应该指代它吗?
    • M... 这只是常见的 C++ 知识。 #include "MainWindow.h" 和 MainWindow* getMainWindow();应该在文件顶部的某个地方。这表明你还有很长的路要走,才能真正将 Qt 用于嵌入式应用程序。
    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多