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

alignment for QCheckBox in QTableWidget



  • I've created a checkbox in a cellwidget within a tablewidget. The checkbox is by default aligned left, like the image attached I can't find any documentation in QCheckBox to set alignment.

    Any help? Is there some sort of spacing or padding for qtablewidget?

    Thanks

    cow

    Capture.JPG


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How did you create them ?



  • I think something reproducible would be:

    ccHeaders = QtWidgets.QTableWidget()
    checkbox = QtWidgets.QCheckBox()
    ccHeaders.setCellWidget(0, 0, checkbox)


  • Banned

    The alignment is not part of checkbox that is why you cannot find it -- the alignment feature is actually part of tablewidget and more precisely that column of your tablewidget



  • @Denni-0 said in alignment for QCheckBox in QTableWidget:

    The alignment is not part of checkbox that is why you cannot find it -- the alignment feature is actually part of tablewidget and more precisely that column of your tablewidget

    I looked for alignment in qtablewidget also, and couldn't find anything: https://doc.qt.io/qtforpython/PySide2/QtWidgets/QTableWidget.html

    I know set alignment is in a tablewidgetitem, but this isn't considered an item, right?


  • Banned

    A quick google search turned up this:

    from QtCore import Qt
    from QtGui  import QStandardItemModel
    
    self.model = QStandardItemModel()
    self.model.setData(self.model.index(0, 0), Qt.AlignRight, Qt.TextAlignmentRole)
    

    I would have to search my stuff to find it but I know I found out how to set an entire column in QTreeView to be centered so I know it can be done but I think I did via the Model I am using in conjunction with my QTreeView --- if I come across that I will post it as well but this should get you headed in the right direction


  • Lifetime Qt Champion

    Why not use the Qt.ItemIsUserCheckable flag to get the checkboxes you want rather than widgets San you are doing ?

    Widgets set like that will not respond to any role because they are not part of the rendering of the model.


  • Banned

    @SGaist would not that kind of depend on how the Model was actually defined -- my example was not meant to be the whole answer just enough to point the OP in a direction to help them solve their issue

    I have to admit I am operating off of pure memory which can be at times a bit faulty ;-)


  • Lifetime Qt Champion

    Nop, using setIndexWidget setCellWidget puts a widget on that cell but it does not care about the model content.

    The documentation clearly indicates that it should only be used on special cases for static content and not like done currently to show widgets on each and every line.


  • Banned

    @SGaist I do not think a mentioned anything about setIndexWidget?? where did that come into play? And again I have actually done this in a QTree so I know it can be done on a column by column basis -- I will see if I can find that code example I made


  • Lifetime Qt Champion

    You didn't (and it was setCellWidget, my bad) but the OP did.
    It comes from the original post



  • @SGaist said in alignment for QCheckBox in QTableWidget:

    Why not use the Qt.ItemIsUserCheckable flag to get the checkboxes you want rather than widgets San you are doing ?

    Widgets set like that will not respond to any role because they are not part of the rendering of the model.

    I originally went with this and had issues with how I coded it, so I went with the checkbox route. But a clear head does wonders, and I did it this way. Thank you guys.



  • I know this topic is marked solved, but I can't figure out what anyone is saying the solution is. So to be helpful I suggest using setSyleSheet().

    For example:
    QCheckBox * cb = new QCheckBox("Show");
    cb->setStyleSheet("padding-left: 10px;");
    tileInfoTable->setCellWidget(TI_SHOW,col,cb);


Log in to reply