For common QWidget controls, we can easily use the nesting between QLayout, QBoxLayout, and QHBoxLayout to complete the beautiful layout of many system controls, but for custom controls, we will feel very tangled, because QGraphicsLayoutItem and QGraphicsLayout cannot add custom controls. Only the addItem of QGraphicsLinearLayout can add elements of the QGraphicsLayoutItem type, either layout or control ), we found that the following inheritance relationship exists: 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16222U252-0.png "title =" .png "/>
In this way, you can use QGraphicsWidget to add controls in the middle of the scenario. After using this method, you do not need to add controls to scene, you only need to add widgets containing layout to scene in the layout at one time.
QGraphicsLinearLayout *linearLayout = new QGraphicsLinearLayout(Qt::Vertical);QGraphicsWidget *widget = new QGraphicsWidget;linearLayout->addItem(linkLabel);linearLayout->addItem(appLabel);widget->setLayout(linearLayout);scene->addItem(widget);
Here, linkLabel and appLabel are our two custom controls, and scene is the scenario. However, the controls added in this way may have some problems. For convenience, provides all the code for the main function.
# Include <QtGui> # include <QGraphicsLayoutItem> # include "showlabel. h "# include" clickhandler. h "int main (int argc, char * argv []) {QApplication app (argc, argv); QGraphicsScene * scene = new QGraphicsScene; QGraphicsView * view = new QGraphicsView (scene ); showLabel * linkLabel = new ShowLabel ("www.hao123.com"); // set the font QFont linkFont ("Arial", 20, QFont: Bold, true ); linkLabel-> setFont (linkFont); // set the underline linkLabel-> setUnderline (true); // set the color QColor linkColor (255, 0, 0 ); linkLabel-> setColor (linkColor); ShowLabel * appLabel = new ShowLabel ("open app"); // set the font QFont appFont ("times", 20, QFont: Bold, true); appLabel-> setFont (appFont); // set the underline appLabel-> setUnderline (true); // set the color QColor appColor (0,255, 0 ); appLabel-> setColor (appColor); // set the widget size linkLabel-> setPreferredSize (rectLink. width (), rectLink. height (); ClickHandler * clickHandler = new ClickHandler (view); QObject: connect (linkLabel, SIGNAL (clicked (), clickHandler, SLOT (on_linkOpen ())); QObject: connect (appLabel, SIGNAL (clicked (), clickHandler, SLOT (on_messageBox (); QRectF rectApp = appLabel-> getRect (); // appLabel-> setMaximumSize (rectApp. width (), rectApp. height (); // set the scene size scene> setSceneRect (0, 0,300,300); QGraphicsLinearLayout * linearLayout = new QGraphicsLinearLayout (Qt: Vertical ); QGraphicsWidget * widget = new QGraphicsWidget; linearLayout-> addItem (linkLabel); linearLayout-> addItem (appLabel); widget-> setLayout (linearLayout); scene-> addItem (widget ); QRect rt = linkLabel-> getRect (); QRect lt = appLabel-> getRect (); // you can specify the size of the view. The value is 300,300 ); view-> rollback (Qt: ScrollBarAlwaysOff); view-> setVerticalScrollBarPolicy (Qt: ScrollBarAlwaysOff); view-> setWindowTitle (QObject: tr ("My hao123 Label ")); view-> show (); return app.exe c ();}
If you do this, the following errors will occur:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16222R439-1.jpg "title =" 16923-5af5215a4071d77ca8502a4d6a41949e.jpg "/>
As you may see, although the following row only has the width of the character "open app", it is stretched because the layout is made in Qt: Vertical mode, the system will automatically focus only on the height of the component, and the width of the component will be aligned with the previous control. I think the horizontal layout should be similar, therefore, you need to add the following code to control the size of the following controls.
appLabel->setMaximumSize(rectApp.width(), rectApp.height());
This is correct,
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16222V4Q-2.jpg "title =" 16945-b3951ff39254e0d1d6eb85a424e43c30.jpg "/>
Other code is provided here.
ShowLabel. h
#pragma once#ifndef SHOWLABEL_H#define SHOWLABEL_H#include <QGraphicsWidget>class QGraphicsSceneMouseEvent;class QGraphicsSceneHoverEvent;class ShowLabel: public QGraphicsWidget{ Q_OBJECTpublic: ShowLabel(const QString &string); void setText(const QString &string); QString text(); void setFont(const QFont& font); void setColor(const QColor& color); void setUnderline(bool isUnderLine); QRect getRect();signals: void clicked();protected: void mousePressEvent(QGraphicsSceneMouseEvent *e); void mouseReleaseEvent(QGraphicsSceneMouseEvent *e); void hoverEnterEvent(QGraphicsSceneHoverEvent *e); void hoverLeaveEvent();protected: virtual QRectF boundingRect(); virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);private: QString m_text; bool m_isPressed; bool m_isUnderLine; QFont m_font; QColor m_penstyle;};#endif //SHOWLABEL_Hsho
ShowLabel. cpp
#include <QDesktopServices>#include <QUrl>#include <QGraphicsSceneMouseEvent>#include <QGraphicsSceneHoverEvent>#include <QPainter>#include <QStyleOptionGraphicsItem>#include "showlabel.h"#include "clickhandler.h"ShowLabel::ShowLabel(const QString &string): m_isPressed(false), m_isUnderLine(false){ m_text = string; setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); setAcceptHoverEvents(true);}void ShowLabel::setText(const QString &string){ if ( m_text != string) { m_text = string; update(); } }void ShowLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); setUnderline(m_isUnderLine); painter->setFont(m_font); painter->setPen(m_penstyle); QRect textRect = boundingRect().toRect(); painter->fillRect(rect(), (0, 0, 255)); painter->drawText(textRect, Qt::AlignLeft, m_text);}void ShowLabel::setFont(const QFont& ft){ m_font = ft;}void ShowLabel::setColor(const QColor& color){ m_penstyle = color;}void ShowLabel::mousePressEvent(QGraphicsSceneMouseEvent *e){ if (e->button() == Qt::LeftButton) { m_isPressed = true; setCursor(Qt::ArrowCursor); }}void ShowLabel::mouseReleaseEvent(QGraphicsSceneMouseEvent *e){ if (e->button() == Qt::LeftButton && m_isPressed && m_isUnderLine) { emit clicked(); setCursor(Qt::PointingHandCursor); m_isPressed = false; }}void ShowLabel::hoverEnterEvent(QGraphicsSceneHoverEvent *){ setCursor(Qt::PointingHandCursor);}void ShowLabel::hoverLeaveEvent(){ setCursor(Qt::ArrowCursor);}QRectF ShowLabel::boundingRect(){ QFontMetrics metrics(m_font); qreal width = metrics.width(m_text); qreal height = metrics.height(); return QRectF(0,0,width,height);}void ShowLabel::setUnderline( bool isUnderLine ){ m_isUnderLine = isUnderLine; m_font.setUnderline(true);}QRect ShowLabel::getRect(){ return boundingRect().toRect();}QString ShowLabel::text(){ return m_text;}
Clickhandler. h
#include <QUrl>class ClickHandler : public QObject{ Q_OBJECT;public: ClickHandler(QObject* parent);private slots: void on_linkOpen(); void on_messageBox();};
Clickhandler. cpp
#include <QDesktopServices>#include <QMessageBox>#include "showlabel.h"#include "clickhandler.h"void ClickHandler::on_linkOpen(){ ShowLabel* label = qobject_cast<ShowLabel*>(sender()); QString text = label->text(); QDesktopServices::openUrl(text);}void ClickHandler::on_messageBox(){ ShowLabel* label = qobject_cast<ShowLabel*>(sender()); QString text = label->text(); QMessageBox::about(NULL, "msg box", text);}ClickHandler::ClickHandler( QObject* parent ): QObject(parent){}
This article from the "selling cute programmers" blog, please be sure to keep this source http://7677869.blog.51cto.com/7667869/1264729