Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why does QTableWidgetItem setflags() trigger itemChanged() signal?



  • void Dialog::setRowDisable(QTableWidget* table, int row){
        for(int column = 0; column < table->columnCount(); ++column){
            auto item = table->item(row, column);
            item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        }
    }
    

    This simple code makes me suffer from segmentation fault derived from infinite loop.
    0_1534590188963_capture.PNG
    I can not understand this situation because setting flag does not affect the data of QTableWidgetItem.
    Is it intended? If so, how can I prevent it from triggering the event?


  • Lifetime Qt Champion

    Hi
    Because you do change the item. :)
    Anyway, try
    setting

    void Dialog::setRowDisable(QTableWidget* table, int row){
       table->blockSignals(true);
        for(int column = 0; column < table->columnCount(); ++column){
            auto item = table->item(row, column);
            item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        }
       table->blockSignals(false);
    }
    

    If that works as you want , have a look at
    https://doc.qt.io/qt-5/qsignalblocker.html

    Its a better way of doing the same.



  • @mrjj Oh, there is a blockSignal function in Qt. I was struggling with implementing it myself ;) Thanks!


  • Lifetime Qt Champion

    @Aaron-Kim
    yep :)
    qsignalblocker is a special scope handling class that ensure u dont miss
    a blockSignals(false); if code in between can bail out due to error.


Log in to reply