Toggle Checked state on a QTreeWidget



  • Hi,

    I have a QTreeWidget subclass that I am loading on it just the names of the files currently loaded in the application. Now I need to be able to check or uncheck these items in the "list" so for example if there are two items in the list and both are checked, then both will be displayed in my application, if only one is checked, the checked one will be displayed on the application, etc..

    For this in my function called insertFileName() I make a QTreeWidgetItem and set its flags to like this:

    @
    QTreeWidgetItem* item = new QTreeWidgetItem(this);
    item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |Qt::ItemIsEnabled);
    @

    Then in one I add the item to the tree doing somthing like this making sure I insert the item in an empty row:

    @
    item->setText(0, fName);
    item->setCheckState(0, Qt::Checked);
    item->setText(1, stype);
    insertTopLevelItem(i, item);
    @
    stype is just a string I get from somewhere else.
    Now for some reason I cant toggle the checked state. This runs and the items appear with the checkbox checked but when I try to uncheck it nothing happens. I tried doing something in the mousepressEvent to be able to toggle and it works but offcourse it toggles no matter where I press in the treeWidget.

    What am I missing and need to set or enable to be able to toggle the checked state of each item I add to the tree?
    If there is no easy way, in the mousePressevent how could I make the state changed depending on which item I clicked on?

    MAny thanks for the help!



  • You must set Qt::ItemIsEditable.



  • Thanks for the reply,

    Yes I tried that actually, setting it doesn't change anything in this case. I actually have it like this:

    @
    item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |Qt::ItemIsEnabled | Qt::ItemIsEditable);
    @

    I load every item, and they all load checked like I set it to, but I cant uncheck them for some reason...



  • That's indeed strange... can you please write down a small, minimal testcase we can see?



  • Sure, so when I load a file I get the fileName and then call the insertFileName() function which is a function of the QTreeWidget subclass to have the name of the file listed in the tree as I load, This function is:

    @

    void myTreeWidget::insertFileName(QString fileName)
    {
    int i = 0;
    bool full = true;

    QTreeWidgetItem* item = new QTreeWidgetItem(this);
    item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEditable |Qt::ItemIsEnabled | Qt::ItemIsSelectable);

    QString fName = withoutPath(fileName);
    QString stype = 'String';
    
    while(full)
    {
        if(topLevelItem(i))
            i++;
        else
        {
            item->setText(0, fName);
            item->setCheckState(0, Qt::Checked);
            item->setText(1, stype);
            insertTopLevelItem(i, item);
            setCurrentItem(item, 0);
            full = false;
        }
    }
    

    }
    @

    So this loads the files fine but can't uncheck any of the loaded files. The point of being able to check or uncheck is that with that I want to update the display of my application so I need to be able to set checked (to make visible) or viceversa..


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.