2

我正在尝试通过子类化并覆盖paintEvent来自定义QPUshButton。我正在写文本,然后是如下图标:

paintEvent(QPaintEvent *paint)
    {
         QStyleOption opt;
         opt.init(this);
         QPainter p(this);
        //Draw the base
         style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

        //Draw the text
         style()->drawItemText(&p,this->rect(),Qt::AlignCenter,(this->palette()), true, this->text());

    //How do I make the image immediately follow the text

         if(!this->icon().isNull())
            //Draw the icon at 75% button height
            style()->drawItemPixmap(&p, this->rect(),Qt::AlignRight|Qt::AlignVCenter, this->icon().pixmap(this->rect().height()  * 0.75));

    }

我居中对齐文本,右对齐图标。但是,这会导致文本和图标之间出现间隙。有什么办法让我在文本之后立即绘制图标,而不是对齐?

换句话说,有没有办法获得drawItemText完成的位置?

4

2 回答 2

2

QStyle::itemTextRect()将告诉你文本将在给定的矩形、字体度量和对齐方式下放置在哪里。

于 2012-09-12T17:04:24.263 回答
0

QFontMetrics会告诉你你的文本有多宽。它不知道您的矩形,因此您必须自己进行对齐计算。

于 2012-09-12T15:31:59.097 回答