Drawing a line on a QWidget

13,583

Solution 1

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();
myFrame->setFrameShape(QFrame::HLine);

const int NUMBER_OF_COLUMNS_IN_GRID = 4;
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.

Solution 2

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.

Share:
13,583

Related videos on Youtube

Admin
Author by

Admin

Updated on June 10, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

    #ifndef START_MENU_SECTION_FOOTER_H
    #define START_MENU_SECTION_FOOTER_H
    
    #include <QWidget>
    
    class QHBoxLayout;
    class QPainEvent;
    
    class StartMenuSectionFooter : public QWidget
    {
      Q_OBJECT
    
      QBHoxLayout *layout;
    
      void paintEvent( QPainEvent *e );
    
    public:
      StartMenuSectionFooter( QWidget *parent = NULL );
    };
    
    #endif
    
    • RA.
      RA. almost 12 years
      Have you tried using a QFrame with the frame shape set to HLine?