Important: Please read the Qt Code of Conduct -

Disable a treewidget but allow scrollbars

  • Hi

    I have a QTreeWidget which has to be disabled but the user should be allowed to see the contents of it by providing scroll-bars. I have tried the following solution, with this I am able to see scroll-bar but it's not moving.

    View::View( QWidget* parent ) 
    bool View::eventFilter( QObject *obj, QEvent *event )
    	if( event->type() == QEvent::EnabledChange )
    		return true;
    		return QObject::eventFilter(obj, event);

    can anyone please suggest a solution. thanks in advance.

  • Moderators

    Hi @ran_s, and welcome to the Qt Dev Net!

    It sounds like you want to disable the items, but not the QTreeWidget itself. One way to to that is to remove the Qt::ItemIsEnabled flag from the QTreeWidgetItem:

    // Construct objects
    auto widget = new QTreeWidget;
    auto item = new QTreeWidgetItem(strings);
    // ...
    // Edit flags
    Qt::ItemFlags currentFlags = item->flags();
    item->setFlags(currentFlags & ~Qt::ItemIsEnabled);
    // Add item to widget

  • @JKSH thank you for your suggestion, it solves my problem.
    as i was using QTreeView I could able to set flags by overriding QAbstractItemModel::flags()
    it is working fine.

Log in to reply