Hide/Remove preview widget from QColumnView?



  • Is there any way to do this? Looks like this functionality is not provided in QColumnView. Not everybody would like to have previews and it's not suitable for all cases.



  • Not sure but I would try:
    connect(columnView,&QColumnView::updatePreviewWidget,[=]()->void{columnView->setPreviewWidget(nullptr);});



  • @VRonin Setting None in python for Preview Widget (equivalent of nullptr) results in crash. Added the code below. If possible, Can you please check the same in C++ and Qt? Thanks in advance.

    from PyQt4 import QtCore, QtGui
    import os
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        def _fromUtf8(s):
            return s
    
    try:
        _encoding = QtGui.QApplication.UnicodeUTF8
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig)
    
    class MyModel(QtGui.QFileSystemModel):
        def __init__(self):
            super().__init__()
                            
        def hasChildren(self,index):
            hasChildren=super().hasChildren(index)
            path=super().filePath(index)
            
            dirIter=QtCore.QDirIterator(path,QtCore.QDir.AllDirs|QtCore.QDir.NoDotAndDotDot|QtCore.QDir.NoSymLinks)
            if dirIter.hasNext():
                return True
            else:
                return False
            
            return hasChildren
                
    class columnView(QtGui.QDialog):
        def __init__(self,parent=None):
            super().__init__(parent)
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)
            
            self.model=MyModel()
            self.model.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.NoDotAndDotDot|QtCore.QDir.NoSymLinks)
            path=os.path.expanduser("~")
            self.model.setRootPath(path)
            self.ui.columnView.setModel(self.model)
            
            self.ui.columnView.setRootIndex(self.model.index(path))
            
            self.ui.columnView.updatePreviewWidget.connect(self.closePreview)
            
            self.show()
            
            self.ui.closePushButton.clicked.connect(self.close)
    
        def closePreview(self,index):
            self.ui.columnView.setPreviewWidget(None)
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName(_fromUtf8("Dialog"))
            Dialog.resize(596, 389)
            self.verticalLayout = QtGui.QVBoxLayout(Dialog)
            self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
            self.columnView = QtGui.QColumnView(Dialog)
            self.columnView.setObjectName(_fromUtf8("columnView"))
            self.verticalLayout.addWidget(self.columnView)
            self.horizontalLayout = QtGui.QHBoxLayout()
            self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
            spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
            self.horizontalLayout.addItem(spacerItem)
            self.closePushButton = QtGui.QPushButton(Dialog)
            self.closePushButton.setObjectName(_fromUtf8("closePushButton"))
            self.horizontalLayout.addWidget(self.closePushButton)
            self.verticalLayout.addLayout(self.horizontalLayout)
    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog):
            Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
            self.closePushButton.setText(_translate("Dialog", "Close", None))
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        view = columnView()
        sys.exit(app.exec_())
    

  • Lifetime Qt Champion

    Hi,

    Setting a null preview widget will make it crash and that's normal. IIRC, there's usually a mention in the documentation when it's supported. In this case, the code of QColumnView doesn't show anything in that regard.

    What about just hiding that preview widget ?



  • @SGaist There's no option to provided to hide the previewWidget. Even if no widget is set, the column gets created and displays nothing.



  • @SGaist said in Hide/Remove preview widget from QColumnView?:

    Setting a null preview widget will make it crash and that's normal. IIRC, there's usually a mention in the documentation

    from http://doc.qt.io/qt-5/qcolumnview.html#previewWidget:

    Returns the preview widget, or 0 if there is none.

    I count this as a "mention"



  • There is no way to remove preview column even if no widget was set.



  • Thank you all for replying. Yes looks like there's no way to do this. But when you Google around, you could see many of them find this widget annoying and wanted to turn this off. Hope Qt developers will look into this and provide an option for setVisible(False).


  • Lifetime Qt Champion

    @VRonin While I concur it's mentioned in the getter, I'll understand it as when none has been set yet. The mention I was talking about should be in the getter function. Maybe an update to the doc would make the situation clearer.

    As for the visibility problem, what about myColumnCiew->previewWidget()->hide(); ?



  • @SGaist When we hide() the preview widget, it will hide the widgets if something is set as preview widget. It's not preventing the columview from creating the extra column.


Log in to reply
 

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