B
B
Blunker2015-11-29 08:21:24
Qt
Blunker, 2015-11-29 08:21:24

How to set icon on qpushbutton?

#include <QPushButton>
#include <QWidget>
#include <QHBoxLayout>
#include <QSize>
#include <QGroupBox>
#include <QTextEdit>
#include <QIcon>
#include <QLabel>
#include <QDebug>
#include <QVBoxLayout>

class GameButton;

class GameButton : public QPushButton
{
public:
    explicit GameButton(QWidget *parent = 0) : QPushButton(parent) {}
protected:
    bool player;
};

class MainWindow : public QWidget
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    QSize mainFrame, SizeButton, sizeChat, sizeEdit;
    GameButton *but;
    QHBoxLayout *firstLine, *mainLayout, *secondLine, *thirdLine;
    QVBoxLayout *left, *right;
    QTextEdit *edit;
    QLabel *MainChat;
public slots:
    void setupPicture_0();

};

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    mainFrame.setHeight(400);
    mainFrame.setWidth(600);
    SizeButton.setHeight(50);
    SizeButton.setWidth(50);
    sizeChat.setHeight(300);
    sizeChat.setWidth(200);
    sizeEdit.setHeight(30);
    sizeEdit.setWidth(200);
    but = new GameButton[9];
    /*______________________*/

    /*Редакирование окна*/
    this->setFixedSize(mainFrame);
    this->setWindowTitle("erert");
    /*______________________________*/
    for (int i = 0; i < 9; i++)
        but[i].setFixedSize(SizeButton);
    /*________________________*/

    firstLine = new QHBoxLayout;
    left = new QVBoxLayout;
    right = new QVBoxLayout;
    mainLayout = new QHBoxLayout;
    edit = new QTextEdit;
    secondLine = new QHBoxLayout;
    thirdLine = new QHBoxLayout;
    MainChat = new QLabel;
    /*__________________________*/

    firstLine->addWidget(&but[0]);
    firstLine->addWidget(&but[1]);
    firstLine->addWidget(&but[2]);
    /*__________________________*/

    secondLine->addWidget(&but[3]);
    secondLine->addWidget(&but[4]);
    secondLine->addWidget(&but[5]);
    /*___________________________*/
    thirdLine->addWidget(&but[6]);
    thirdLine->addWidget(&but[7]);
    thirdLine->addWidget(&but[8]);
    /*__________________________*/

    MainChat->setFixedSize(sizeChat);
    MainChat->setStyleSheet("color: rgb(50, 0, 70)");
    MainChat->setText(">Player1: Hello! \n>Player2: Hello!!!\n");
    edit->setFixedSize(sizeEdit);

    right->addWidget(MainChat);
    right->addWidget(edit);
    left->addLayout(firstLine);
    left->addLayout(secondLine);
    left->addLayout(thirdLine);

    mainLayout->addLayout(left);
    mainLayout->addLayout(right);

    this->setLayout(mainLayout);
    /*_____________________*/

    connect(&but[0], SIGNAL(clicked()), this, SLOT(setupPicture_0()));

}

MainWindow::~MainWindow()
{
}

void MainWindow::setupPicture_0()
{
    but[0].setIcon(QIcon("1.png"));
}

For some reason, when clicking on the button, the icon on the button is not set. What is the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly Medvedev, 2015-11-29
@balamyt92

Try to install like this.

QPixmap pixmap("image_path");
QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());

V
Vitaly, 2015-11-29
@vt4a2h

You need to check (in this order):
1) Is the slot actually activated, if not, call qmake.
2) Try the full path to the icon, if it works, then the file you have is not next to the executable file and you need to move it.
And globally, discover *.ui files (just create a Qt Widgets Application project) and resource files. In your example, I don't see anything that would require you to write code by hand and not throw in a GUI in a visual editor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question