Showing grid lines in a QGridLayout



  • Hi all,
    I was wondering if there is an easy way to show the grid lines separating sections in a grid layout governed by the QGridLayout class.
    The doc has been failing me so far but I still feel there is an easy way to achieve this.

    Thanks


  • Moderators

    i wouldn't count on it ... since QGridLayout is not a QWidget and thus can't draw anything ...
    The only way i see is that you insert your own "line widgets" between the widgets you enter in the grid layout. But this is alot of extra work though...



  • Thanks raven-worx,
    What do you exactly mean by ''line widget".I was thinking of subclassing QWidget ,draw the lines I want on the widget in the paint override and then apply the layout on my custom widget.Some people are even proposing to use QTableWidget and insert the widgets in the cells.I am going to try all these options but suggestions are always more than welcome.

    Thanks.


  • Moderators

    [quote author="musimbate" date="1370246350"]
    What do you exactly mean by ''line widget".
    [/quote]
    you create a custom VLineWidget, HLineWidget, CrossWidget, which just paint their lines and insert them along with your other widgets to the grid layout.

    [quote author="musimbate" date="1370246350"]
    I was thinking of subclassing QWidget ,draw the lines I want on the widget in the paint override and then apply the layout on my custom widget.
    [/quote]
    i'm not sure if that will work as you want. Determining the positions for the lines could be quite difficult.

    [quote author="musimbate" date="1370246350"]
    Some people are even proposing to use QTableWidget and insert the widgets in the cells.I am going to try all these options but suggestions are always more than welcome.[/quote]
    This is by far the most easiest way to go ;)



  • [quote author="raven-worx" date="1370246783"]
    i'm not sure if that will work as you want. Determining the positions for the lines could be quite difficult.
    [/quote]
    Was starting to face that problem.Will try your "line widget idea " and QTable and see what works best.
    Thanks for your input!:-)



  • [quote author="raven-worx" date="1370246783"]
    you create a custom VLineWidget, HLineWidget, CrossWidget, which just paint their lines and insert them along with your other widgets to the grid layout.
    [/quote]

    Another way could be to create a ContentWidget-Class for every cell which contains the widget that you want to show in the cell and draws a border around it...
    Perhaps this will simplify the layout ;)



  • Not sure if this would be helpful, but the designer already shows those gridlines.



  • May be something like !http://i.imgur.com/GoBYzTj.png(this?)!

    @ QGridLayout *layout1 = new QGridLayout;
    layout1->addWidget(labelMateria, 0, 0, Qt::AlignRight);
    layout1->addWidget(lineEditMateria, 0, 1, 1, 3);
    layout1->addWidget(labelAnyo, 1, 0, Qt::AlignRight);
    layout1->addWidget(comboBoxAnyo, 1, 1);
    layout1->addWidget(labelColor, 1, 3, Qt::AlignLeft);
    layout1->addWidget(botonColor, 1, 2);
    layout1->addWidget(labelHsCatedra, 2, 0, Qt::AlignRight);
    layout1->addWidget(spinBoxHsCatedra, 2, 1);
    QFrame *linea1 = new QFrame(this); // <<< this does the trick
    linea1->setLineWidth(2);
    linea1->setMidLineWidth(1);
    linea1->setFrameShape(QFrame::HLine);
    linea1->setFrameShadow(QFrame::Raised);
    layout1->addWidget(linea1, 3, 0, 1, 4);
    layout1->addWidget(botonCancelar, 4, 2);
    layout1->addWidget(botonAceptar, 4, 3);

    setLayout(layout1);@


  • If you just need to paint a grid, subclass QWidget and draw your grid inside paintEvent.



  • Thanks guys for all your input.I have tried many options and I like to have many ways to achieve the same thing:-).One question about *Marcos Modenesi *'s solution though:how to control the color of the lines drawn using QFrame.I have tried to apply a palette to the frame but I am getting weird looking lines.Any ideas?
    Thanks again!



  • [quote author="Flavio Tordini" date="1370323386"]If you just need to paint a grid, subclass QWidget and draw your grid inside paintEvent.[/quote]

    Thanks Flavio,but I want the full functionality of a QGridLayout with grid lines shown.VLines and HLines using QFrame seem be getting me close to the desired effect.



  • This works in my example:

    linea1->setPalette(QPalette(QColor(255, 0, 0)));

    @
    QFrame *linea1 = new QFrame(this);
    linea1->setLineWidth(2);
    linea1->setMidLineWidth(1);
    linea1->setFrameShape(QFrame::HLine);
    linea1->setFrameShadow(QFrame::Raised);
    linea1->setPalette(QPalette(QColor(255, 0, 0))); // here
    @

    !http://i.imgur.com/1CKZjUs.png(color red)!



  • Thanks Markos,I have tried that and I am still getting lines with the default color.Guess I ll have to keep looking .Thanks again.
    Dan.


Log in to reply
 

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