Results 1 to 3 of 3

Thread: adjust QLabel according to the size of the window

  1. #1
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default adjust QLabel according to the size of the window

    Hello,
    i have an image and i want to display it but i don't want to fix the size of a QLabel, i want the QPixmap to be adjusted automatically with the QLabel and the QLabel also be adjusted dynamically according to the size of the window, i.e i want the QLabel to resize itself according to the resize of my QMainWindow in such way the QPixmap is also adjusted with QLabel
    How can i do that please.
    ImageViewer.h
    Qt Code:
    1. #ifndef IMAGEVIEWER_H
    2. #define IMAGEVIEWER_H
    3. #include<QtWidgets>
    4.  
    5. class ImageViewer : public QMainWindow
    6. {
    7. Q_OBJECT
    8. public:
    9. ImageViewer();
    10. public slots:
    11. void ouvrir();
    12. private:
    13. QLabel *label;
    14. QImage *image;
    15. QVBoxLayout *layout;
    16. };
    17.  
    18. #endif // IMAGEVIEWER_H
    To copy to clipboard, switch view to plain text mode 
    imageViewer.cpp
    Qt Code:
    1. #include "imageviewer.h"
    2.  
    3. ImageViewer::ImageViewer(): QMainWindow()
    4. {
    5. label = new QLabel(this);
    6.  
    7. label->move(100,100);
    8. layout = new QVBoxLayout(this);
    9. layout->addWidget(label);
    10. this->setLayout(layout);
    11. QMenu *fichier = menuBar()->addMenu("Fichier");
    12. QMenu *edition = menuBar()->addMenu("Edition");
    13.  
    14. QAction *ouvrir = new QAction("Ouvrir",this);
    15. fichier->addAction(ouvrir);
    16. connect(ouvrir,SIGNAL(triggered(bool)),this,SLOT(ouvrir()));
    17.  
    18. }
    19.  
    20. void ImageViewer::ouvrir()
    21. {
    22. QString nomFichier = QFileDialog::getOpenFileName(this,"Open file", "C://");
    23.  
    24. QPixmap p(nomFichier);
    25. label->setPixmap(nomFichier);
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 
    thank you

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

    Default Re: adjust QLabel according to the size of the window

    QMainWindow already has a layout, set your label as the window's centralWidget.

    If you want the label to scale its contents, set "scaledContents" to true.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    rafik (14th February 2016)

  4. #3
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: adjust QLabel according to the size of the window

    Thank you Mr anda for your help, it resizes dynamically now when i expand the size, but when i want to resize into smaller size it didn't resize, are the any solutions if possible?
    ImageViewer.cpp
    Qt Code:
    1. #include "imageviewer.h"
    2.  
    3. ImageViewer::ImageViewer(): QMainWindow()
    4. {
    5. QWidget *centralArea = new QWidget(this);
    6. this->setCentralWidget(centralArea);
    7. label = new QLabel(this);
    8. bouton = new QPushButton("ok", this);
    9. layout = new QVBoxLayout(centralArea);
    10.  
    11. layout->addWidget(label);
    12. layout->addWidget(bouton);
    13. centralArea->setLayout(layout);
    14.  
    15.  
    16. QMenu *fichier = menuBar()->addMenu("Fichier");
    17. QMenu *edition = menuBar()->addMenu("Edition");
    18.  
    19. QAction *ouvrir = new QAction("Ouvrir",this);
    20. fichier->addAction(ouvrir);
    21. connect(ouvrir,SIGNAL(triggered(bool)),this,SLOT(ouvrir()));
    22.  
    23. }
    24.  
    25. void ImageViewer::ouvrir()
    26. {
    27. QString nomFichier = QFileDialog::getOpenFileName(this,"Open file", "C://");
    28.  
    29. QPixmap p(nomFichier);
    30. label->setPixmap(nomFichier);
    31. label->setScaledContents(true);
    32. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rafik; 14th February 2016 at 19:35.

Similar Threads

  1. adjust table size in QDockWidget
    By jackajack01 in forum Qt Programming
    Replies: 0
    Last Post: 13th August 2012, 21:50
  2. Replies: 1
    Last Post: 18th October 2011, 12:02
  3. I can not adjust the size of a widget to your content.
    By jjcarles in forum Qt Programming
    Replies: 3
    Last Post: 8th April 2010, 23:54
  4. trying to adjust size of image in a qlabel
    By mmm286 in forum Newbie
    Replies: 1
    Last Post: 5th November 2009, 07:43
  5. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 15:33

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.