Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Label & Button with Graphic



  • I need some Help with displaying an image (png, jpeg…etc) in a Label and a Button

    I Looked at, and tried a dozen approaches from the samples/tut’s and internet but, no luck! Shouldn’t be this difficult… Seems quite a few folks have this same trouble.

    Coming to QT (Creator) from Java/C++… many others. My usual approach to learn is by similarity via taking a basic idea of a Window with a Button and some Labels and go from there.

    [in Java, it’s as simple as label.setIcon(…) ]

    Using QT5/Creator.

    Any suggestions appreciated, Thanks

    Here’s the code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    ui->label->setText("<b><i>M</i></b>eow!"); // this works
    ui->lcdNumber->display(("35807.4")); // this works

     // does NOT work------------
    ui->pushButton->setIcon( the path to the file?);
     //--------------------------
    
    // does NOT work------------
    ui->label_2->.... a lot of trials and errors
    //--------------------------
    
    // does NOT work------------
    QPixmap myPixmap ("<img ':/Users/bruce/Programming/QT/Proj_myQT/myTest2/aaa.png' /> some text");
    ui->label_2->setPixmap(myPixmap);
    ui->label_2->show();
    // -------------------------
    

    }//end MainWindow


  • Lifetime Qt Champion

    @outwest said in Label & Button with Graphic:

    ui->pushButton->setIcon( the path to the file?);

    And what is the path? Is it correct?

    This is not a valid path to an image file:

    QPixmap myPixmap ("<img ':/Users/bruce/Programming/QT/Proj_myQT/myTest2/aaa.png' /> some text");
    

    Change it to

    QPixmap myPixmap ("/Users/bruce/Programming/QT/Proj_myQT/myTest2/aaa.png");
    


  • Yes, I tried that in one of the variations. After reading up on QT Resources and learning of the need to add/place the image file in a particular location with respect to QT, it now works.

    Thank your for your quick response


Log in to reply