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 ) 
    {
    	this->horizontalScrollBar()->installEventFilter(this);
    	this->verticalScrollBar()->installEventFilter(this);
    }
    bool View::eventFilter( QObject *obj, QEvent *event )
    {
    	if( event->type() == QEvent::EnabledChange )
    		return true;
    	else 
    		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
    widget->addTopLevelItem(item);
    


  • @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
 

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