Drawing a line on a QWidget

I'm attempting to create a widget that consists of of a single horizontal black line. This widget will be placed in a QGridLayout such that it takes up the entire row, thus acting as a separator. As the widget upon which the layout is installed is resized, I'd like the line to change it's size to keep up. I've checked out a number of options, including QLine, QGraphicsView and QImage, but I can't seem to get the line drawn, never mind get it to resize.

I'm implementing this in it's own class so I can reuse it as and when it's needed. Can anyone please help me with this?

#include "startMenuSectionFooter.h"

#include <QtGui>

StartMenuSectionFooter::StartMenuSectionFooter( QWidget *parent )
  : QWidget( parent )
  layout = new QHBoxLayout( this );

void StartMenuSectionFooter::paintEvent( QPainEvent *e )
  QPointF p1 = QPointF( parentWidget()->height(), 0 );
  QPointF p2 = QPointF( parentWidget()->height(), parentWidget()->width() );

  QPainter painter( this );
  painter.setRenderHint( QPainter::Antialiasing, true );
  painter.setPen( QPen( Qt::black, 10 ) );

  painter.drawLine( p1, p2 );

In this case, parent is the parentQWidget upon which the QGridLayout mentioned earlier is installed.


#include <QWidget>

class QHBoxLayout;
class QPainEvent;

class StartMenuSectionFooter : public QWidget

  QBHoxLayout *layout;

  void paintEvent( QPainEvent *e );

  StartMenuSectionFooter( QWidget *parent = NULL );



The simplest way to create a horizontal line in Qt is to use a QFrame with the frameShape property set to QFrame::HLine. You can then place this frame in your grid layout with the appropriate column span. Here's a simple, contrived example:

QFrame* myFrame = new QFrame();

myGridLayout->addWidget(myFrame, 0, 0, 1, NUMBER_OF_COLUMNS_IN_GRID);

This should do everything you need it do to, including automatically resize when the parent layout resizes. You can also play with the frame's palette to show it in the desired color.

You misspelled QPaintEvent. The mispelling means that your paintEvent() function does not override the base classes' paintEvent().

Because you never use the variable e of "QPainEvent", there is no syntax error.

Need Your Help

href links not opening, bootstrap add open class to dropdown menu when click instead of opening link

javascript html twitter-bootstrap

Any ideas how to fix? This applies to the "Animations" and "Education" links. Click these should open a new page, but bootstrap.min just adds the .open class.

How to use Dynamic Instantiation with ICriteria in Nhibernate?

c# nhibernate hibernate nhibernate-mapping hbm

I know that you can do this easily with HQL using the following syntax: