How does the QT button load background images?

Source: Internet
Author: User

A simple code for loading button images may cause very small numbers of Loaded Images. How can this problem be solved? Let's take a look at the following code. I will explain some of the confusion. At the same time, we will also learn some other functions during the process of learning this code. How to display Chinese characters, how to set fonts, and colors.

# Include <qapplication>

# Include <qpushbutton>

#include <QLabel>
#include <QGridLayout>
#include <QTextCodec>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
// The following three lines are used to support Chinese
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("gb2312"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("gb2312"));
    QTextCodec::setCodecForTr( QTextCodec::codecForName("gb2312"));
    QWidget *widget = new QWidget();
    widget->setFixedSize(800, 600);
    widget->move(200,200);
    QPixmap *pixmap = NULL;
    pixmap = new QPixmap(200, 150);
    pixmap->load(":/castle.jpg");
    QIcon *icon = new QIcon(*pixmap);
    QPushButton *button = new QPushButton(*icon, "", widget);
// The following code is the key to image loading, because the size of the default loaded image may differ greatly from the size of the button,
// Adjust the size of the loaded image manually.
    button->setIconSize(QSize(190, 150));
    button->setFixedSize(200, 150);
    QLabel *label = new QLabel(widget);
    QLabel *label1=new QLabel(widget);
    label->setPixmap(*pixmap);
    label->setFixedSize(200, 150);
Label1-> settext ("loading QT button background image ");
// Run the following code to set the font size of label1.
    label1->setStyleSheet("font-size : 53px");
// The following three statements of code can implement the same functions as the above Code, but the difference is that the scope of the following three statements of code is to apply the font of the entire interface.
    //QFont font  = app .font();
      //font.setPointSize(53);
      //app.setFont(font);
// Set the label1 color
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::blue);
    label1->setPalette(pe);
    QGridLayout *layout=new QGridLayout;
    layout->addWidget(button,0,0);
    layout->addWidget(label,0,1);
    layout->addWidget(label1,1,0,2,1);
    widget->setLayout(layout);
    widget->showNormal();
    return app.exec();
}
The preceding figure shows how to add images to the buttons. Some key code is analyzed and improved after time to make the functions more comprehensive.
As a registered member, Images cannot be uploaded within one week. It is more intuitive to have images.
If you want to reprint it, please indicate the source. You are welcome to share it, learn together, and make progress together.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.