Results 1 to 8 of 8

Thread: Display pictures using label?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2016
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Display pictures using label?

    Hi there,

    I tried to make use of the GUi editor in QT creator and apply images to my project using a label as displayed on youtube. but they haven't seemed to work. Can someone teach me how to add multiple pictures to my project? I also tried to include Qpixmap and link to it that way but they still didn't show up when I ran my project.

    I have attached a picture of what I would like it to look like and my project code is currently as follows

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "QPixmap"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::on_pushButton_clicked()
    18. {
    19. ui->label_5->setText("Pump On");
    20. }
    21.  
    22. void MainWindow::on_pushButton_2_clicked()
    23. {
    24. ui->label_6->setText("Pump On");
    25. }
    26.  
    27. void MainWindow::on_pushButton_3_clicked()
    28. {
    29. ui->label_7->setText("Pump On");
    30. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Display pictures using label?

    In Designer you can specify either a file name on disk or from the resource system.

    In code that is basically the same, you can pass a filename from disk or the resource system to QPixmap and put that on the label.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2016
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Display pictures using label?

    Yep, understand that, and have done that both ways passing the filename in code using Qpixmap and in designer specifying from file in there, but when i run the project the pictures don't load.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Display pictures using label?

    Have you checked the paths?

    If they are relative paths the current directory could be different.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Display pictures using label?

    It is generally better to learn how to embed small images like icons into the resource (qrc) file so they get compiled into your program and don't depend on external disk files. As the docs say, when compiling, the image files usually need to be in the same directory as the qrc file, but once compiled and linked into a binary (exe or lib/dll) they get loaded at runtime from the binary.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Sep 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display pictures using label?

    Try something like this:

    Qt Code:
    1. QPixmap pictureToDisplay = QPixmap("This/is/your/path.jpg");
    2. //scale it if you want to
    3. QPixmap scaledImage = pictureToDisplay.scaled(ui->myLabel->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    4. ui->myLabel->setPixmap(scaledImage);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2016
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Display pictures using label?

    Thanks all for your help,

    It is strange no matter if I use code or try add via the designer platform the picture still doesn't want to load.

    I will just continue with the rest of the project and come back to this later, I will post the full code here when ready for help on the picture additions.. Its not project critical that the pictures show.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Display pictures using label?

    Have you verified, e.g. using QFileInfo, that the path is correct?

    Does QimageReader::supportedImageFormats() list "jpg" (or whatever format your images are in)?

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 19th November 2014, 10:35
  2. Qwt display coordinates label
    By Jean-Luc in forum Qwt
    Replies: 2
    Last Post: 13th February 2012, 10:08
  3. display number on label
    By aj2903 in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2009, 07:24
  4. How to display 16 bit grayscale pictures?
    By reimer in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 19:55
  5. Replies: 22
    Last Post: 7th December 2007, 09:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.