Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Label & Button with Graphic
QtWS25 Last Chance

Label & Button with Graphic

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    outwest
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • O outwest

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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");
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • O Offline
        O Offline
        outwest
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved