Drawing a line on a QWidget

女生的网名这么多〃 提交于 2019-12-30 06:11:07

问题


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

回答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.




回答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.



来源:https://stackoverflow.com/questions/11107830/drawing-a-line-on-a-qwidget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!