C++ (Qt)void ToolButton::paintEvent(QPaintEvent * event){ QToolButton::paintEvent(event); QPainter painter(this); QRect rect = event->rect(); QTextOption opt; opt.setAlignment(Qt::AlignCenter); painter.drawText(rect, text_, opt); int y = qAbs(rect.height() - pixmap_.height()) / 2; const int x = 6; painter.drawPixmap(x, y, pixmap_);}
C++ (Qt)while(!asleep()) sheep++;
C++ (Qt) ToolButton * tb = new ToolButton(this); tb->setDrawPixmap(QPixmap(":/images/new.png")); tb->setDrawText("Test");
void QPushButtonPlus::paintEvent(QPaintEvent *event){ QStylePainter StylePainter(this); QStyleOptionButton StyleOption; initStyleOption(&StyleOption); StyleOption.text.clear(); StyleOption.icon = QIcon(); StylePainter.drawControl(QStyle::CE_PushButton, StyleOption); QRect Rect = event->rect(); QTextOption TextOption; TextOption.setAlignment(Qt::AlignCenter); StylePainter.drawText(Rect, text(), TextOption); QPixmap Pixmap = icon().pixmap(iconSize()); int y = qAbs(Rect.height() - Pixmap.height()) / 2; const int x = 6; StylePainter.drawPixmap(x, y, Pixmap);}
void QPushButtonPlus::paintEvent(QPaintEvent *){ // Основа кнопки. QStylePainter StylePainter(this); QStyleOptionButton StyleOption; initStyleOption(&StyleOption); StyleOption.text.clear(); StyleOption.icon = QIcon(); StylePainter.drawControl(QStyle::CE_PushButton, StyleOption); // Прямоугольник для рисования содержимого. QRect SubRect = style()->subElementRect(QStyle::SE_PushButtonContents, &StyleOption, this); // Иконка. if (!icon().isNull()) { QPixmap Pixmap = icon().pixmap(iconSize()); int y = (SubRect.height() - Pixmap.height()) / 2; StylePainter.drawPixmap(SubRect.x(), SubRect.y() + y, Pixmap); SubRect.setX(Pixmap.width() + 2* SubRect.x()); } // Текст. if (!text().isEmpty()) { QTextOption TextOption; TextOption.setAlignment(Qt::AlignCenter); StylePainter.drawText(SubRect, text(), TextOption); }}