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

QGridlayout 2 Column, when Widget hide, auto resize



  • Hello!
    I have widgets in QGridLayout.
    QGridlayout's column size 2, row size variable.
    When i hide 2.button; the 3.'button not going to 2.'button's place. Is there anyway to do this automaticly?
    I don't want to removeWidget because i am trying to do filtering, i have widgets, i can show and hide. When i remove widget; filtering system will not work.
    So Is there anyway to take place of hidding widget's?
    Thank you.
    question.png


  • Qt Champions 2019

    @Emrecp said in QGridlayout 2 Column, when Widget hide, auto resize:

    Is there anyway to do this automaticly?

    Not automatically.
    "I don't want to removeWidget because i am trying to do filtering" - I don't understand this. What filtering do you mean and how is it going to work if you put 3.'button where 2.'button was before?



  • @jsulm Loop in QGridLayout, checking widget's variable, if search QTextEdit's text in widget's variable then i will show widget else i am hiding widget.
    I don't want to removewidget because when search QTextEdit is empty; all widgets must be showen.
    Thanks


  • Qt Champions 2019

    @Emrecp said in QGridlayout 2 Column, when Widget hide, auto resize:

    I don't want to removewidget because when search QTextEdit is empty; all widgets must be showen.

    Then show all of them if text edit is empty - move every widget to the position where it should be if all widgets are shown.



  • @Emrecp
    I too am somewhat lost on what you want to achieve why.

    But as @jsulm says: if you have a QGridLayout, and if you want to hide one of the widgets, and if you then want all the other widgets to move left/up to close up the unoccupied space: then Qt grid won't do that for you, it is your job to move all the necessary widgets around to their new positions. Note how

    void QGridLayout::addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = Qt::Alignment())
    

    allows you to position widgets at specified row/column, you would need to removeWidget() from its current position and then call that to move it to new position. When hidden widget becomes visible again, you would need to shuffle the others back to original positions.

    If you are willing to give up on your exact grid positioning, what you may really want is what I have used in the past. When I had a bunch of filters to show the user, and some of them needed to be hidden/shown from time to time, what I wanted was for them to move around to close up the gap. Qt does not offer this as a layout, but I found the Flow Layout Example. You would have to copy all that code. That gives you a layout where items are places left-to-right, and when they reach the width of the flow panel it grows vertically and they are floated down to the next line below. You don't get an exact "grid" layout, but you get no gaps and it takes up the minimum width/height for the items. Quite a bit of work, but up to you....



  • @jsulm @JonB Thanks for comments.
    I solved with creating another QGridLayout, when search text edit's textChanged, clearing all widgets in first QGridLayout; then I am looping in second QGridLayout (all widgets here), if it's compatible with variable then i am adding to first QGridLayout. Then i show first QGridLayout.

    I was hoping QGridLayout do this automaticly.

    Here is code if someone needs

                # CLEAR WIDGETs
                for i in range(1, self.vbox.count()):
                    if self.vbox.itemAt(i) != None and self.vbox.itemAt(i).widget()!=None:
                        self.vbox.itemAt(i).widget().hide()
                # < CLEAR WIDGETs >
                INDEX = 1
                for i in range( self.QGRID_ALL_WIDGETS.count()):
                    if self.QGRID_ALL_WIDGETS.itemAt(i) != None and self.QGRID_ALL_WIDGETS.itemAt(i).widget() != None:
                        if (str(Ad).lower() in str(SearchText).lower()): #  Ad is my special variable
                            wid.show()
                            self.vbox.addWidget(wid, math.ceil(INDEX / 2), (INDEX - 1) % 2, 1, 1)
                            INDEX += 1
                        else:
                            wid.hide()
    

Log in to reply