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

**How to make qtablewidget Read only?**



  • I have introduces a table in my ui file ,now what I want is that during run time the table values can only change due to internal algorithm but user is unable to make any change or select any cell externally.

    How can I do that?

    i want that table should only update data/text via internal algorithm ,but once the text is updated that cell become accessible to external input

    QTableWidgetItem *itm = new QTableWidgetItem();
    
    itm->setFlags(itm->flags()^(Qt::ItemIsSelectable|Qt::ItemIsEditable));
    ui->tableWidget->setItem(i,j,itm);
    

    at this point user cant enter value externally,but once i do this:

    QTableWidgetItem *itm = new QTableWidgetItem();
    itm->setText("0.554");
    ui->tableWidget->setItem(2,3,itm);
    

    cell(2,3) becomes accessible to external input,how to avoid this thing??

    after browsing many people suggest to use noedittrigger but i am unable to understand how to use it and what it does


  • Lifetime Qt Champion

    @vasu_gupta said in **How to make qtablewidget Read only?**:

    noedittrigger

    Hi
    It is
    pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    Normally you can press enter or F2? or double click to start editing a cell. this will disable that.



  • @mrjj so after this will i be able to change table data by algorithm without making it editable again


  • Lifetime Qt Champion

    @vasu_gupta
    It disables the users normal ways of start editing.

    It has nothing to do with the items as such.

    • ..,but once i do this:

    You must set the flag AGAIN when you create new item.
    That is why it becomes editable again. Its no longer the old item and
    hence its editable again/as normally.

    But why do you new it again ?
    You can just call setText on the existing.



  • But how can i disable editing in only the items that i want?


  • Lifetime Qt Champion

    Hi,

    Use QTableWidget::setFlags for the items you want to have different capabilities.



  • Hi,

    Vnom->setFlags(Qt::ItemIsEditable);
    

    like this?but i want it to not to be editable.


  • Lifetime Qt Champion

    Hi
    Well more like
    Vnom->setFlags( Vnom->flags() & ~Qt::ItemIsEditable);
    To mask out the Edit Flag and keep rest.


Log in to reply