Results 1 to 9 of 9

Thread: How to display a little image near a QSlider

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to display a little image near a QSlider

    Hi to all,
    I would display a little image at the left of a QSlider. The slider change the volume of a song and instead of put the text Volume: near the slider
    I would display a little icon representing the volume so the user can know the meaning of the slider.
    I would layout orizontally the icon and the slider so:

    Qt Code:
    1. m_panPixmap = new QLabel;
    2. QPixmap panPixmap(":/images/pan.png");
    3. panPixmap.scaled(5,5);
    4. m_panPixmap->setPixmap(panPixmap);
    5.  
    6. m_panSlider = new QSlider(Qt::Horizontal);
    7. m_panSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    8. m_panSlider->setMinimum( -100 );
    9. m_panSlider->setMaximum( 100 );
    10. m_panSlider->setValue( 0 );
    11.  
    12. QVBoxLayout panLayout;
    13. panLayout.addWidget(m_panPixmap);
    14. panLayout.addWidget(m_panSlider);
    To copy to clipboard, switch view to plain text mode 

    and then add to the main vertical layout
    Qt Code:
    1. vl->setSpacing(20);
    2. vl->addLayout(ll);
    3. vl->addStretch(1);
    4. vl->addLayout(&panLayout); //<-- this is the pan layout
    5. ..more code
    To copy to clipboard, switch view to plain text mode 

    My problem is that the image is displayed too big and it break the layout. The image and the slider are not displayed correctly.
    How can I solve my problem?

    Best Regards,
    Franco
    Franco Amato

  2. #2
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display a little image near a QSlider

    I did a mistake in the previous post.
    The real code is:

    Qt Code:
    1. QHBoxLayout panLayout; //horizontal and not vertical
    2. panLayout.addWidget(m_panPixmap);
    3. panLayout.addWidget(m_panSlider);
    To copy to clipboard, switch view to plain text mode 

    but in my program the code is correct.
    I don't know how to solve my problem

    Regards,
    Franco Amato

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems addind a simple horizontal layout

    I changed the code inserting a label instead of an image but the result is still wrong.
    I tried to change all without results.
    I post some code:

    Qt Code:
    1. PanelInfo::PanelInfo(QWidget* parent /* = 0 */ )
    2. : QFrame(parent),
    3. m_sound(0)
    4. {
    5. setFrameStyle( QFrame::Box | QFrame::Raised );
    6. setMinimumSize( QSize(150, 240) );
    7. setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
    8.  
    9. /* labels */
    10. m_durationLabel = new QLabel("DURATION");
    11. m_channelsLabel = new QLabel("CHANNELS");
    12. m_freqLabel = new QLabel("FREQ");
    13. m_bitsLabel = new QLabel("BITS");
    14.  
    15. m_durationLabel->setMinimumSize( QSize(100, 20) );
    16. m_channelsLabel->setMinimumSize( QSize(100, 20) );
    17. m_freqLabel->setMinimumSize( QSize(100, 20) );
    18. m_bitsLabel->setMinimumSize( QSize(100, 20) );
    19.  
    20. m_durationLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
    21. m_channelsLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
    22. m_freqLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
    23. m_bitsLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
    24.  
    25. QVBoxLayout* ll = new QVBoxLayout();
    26. ll->setContentsMargins(0, 0, 0, 0);
    27. ll->setSpacing(0);
    28. ll->addWidget( m_durationLabel );
    29. ll->addWidget( m_channelsLabel );
    30. ll->addWidget( m_freqLabel );
    31. ll->addWidget( m_bitsLabel );
    32.  
    33. m_panLabel = new QLabel("Pan"); //<----------THIS IS THE PROBLEM
    34.  
    35. m_panSlider = new QSlider(Qt::Horizontal);
    36. m_panSlider->setMinimumSize(50,20);
    37. m_panSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    38. m_panSlider->setMinimum( -100 );
    39. m_panSlider->setMaximum( 100 );
    40. m_panSlider->setValue( 0 );
    41.  
    42. QHBoxLayout panLayout; //<----this layout holds the label and the slider but the result is odd
    43. panLayout.addWidget(m_panLabel);
    44. panLayout.addWidget(m_panSlider);
    45.  
    46. m_volSlider = new QSlider(Qt::Horizontal);
    47. m_volSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    48. m_volSlider->setMinimum( 0 );
    49. m_volSlider->setMaximum( 100 );
    50. m_volSlider->setValue( 100 );
    51.  
    52. /* spinbox */
    53. m_freqControl = new QDoubleSpinBox();
    54. m_freqControl->setMinimum( 0.0 );
    55. m_freqControl->setMaximum( 3.0 );
    56. m_freqControl->setValue( 1.0 );
    57. m_freqControl->setSingleStep( 0.1 );
    58. m_freqControl->setWrapping(1);
    59.  
    60. QLabel* markerLabel = new QLabel("Markers: ");
    61. QLineEdit *numMarkers = new QLineEdit("0");
    62. numMarkers->setMinimumSize(10,20);
    63. numMarkers->setMaximumWidth(30);
    64. numMarkers->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
    65. numMarkers->setReadOnly(true);
    66. QHBoxLayout* hl = new QHBoxLayout();
    67. hl->setContentsMargins(0, 0, 0, 0);
    68. hl->setSpacing(0);
    69. hl->addWidget( markerLabel );
    70. hl->addWidget( numMarkers );
    71.  
    72. vl->setSpacing(20);
    73. vl->addLayout(ll);
    74. vl->addStretch(1);
    75. vl->addLayout(&panLayout);
    76. vl->addWidget(m_volSlider);
    77. vl->addWidget(m_freqControl);
    78.  
    79. vl->addLayout(hl);
    80. setLayout(vl);
    81. }
    To copy to clipboard, switch view to plain text mode 

    and the result is this image.jpg.
    Where I drawn an arrow you can see that the QSlider is broken and it's painted over the QLabel "pan".
    I really don't understand where my code is wrong.
    I need help.

    Best Regards,
    Franco
    Franco Amato

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems addind a simple horizontal layout

    Once again it is a newbie error you do. At the end of your block your layout get distroyed and so both elements are positioned (0,0). Create your layout on the heap (as you do with the other layoute) and all will be fine.

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems addind a simple horizontal layout

    Quote Originally Posted by Lykurg View Post
    Once again it is a newbie error you do. At the end of your block your layout get distroyed and so both elements are positioned (0,0). Create your layout on the heap (as you do with the other layoute) and all will be fine.
    Yes it works...thank you
    I have a last problem.
    I created a horizontal layout with a Label, a slider and again a label so:
    Qt Code:
    1. QLabel* volLabel = new QLabel("Vol");
    2. m_volValue = new QLabel;
    3. m_volValue->setNum(100);
    4.  
    5. m_volSlider = new QSlider(Qt::Horizontal);
    6. m_volSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    7. m_volSlider->setMinimum( 0 );
    8. m_volSlider->setMaximum( 100 );
    9. m_volSlider->setValue( 100 );
    10.  
    11. QHBoxLayout* volLayout = new QHBoxLayout;
    12. volLayout->setAlignment(Qt::AlignLeft);
    13. volLayout->setSpacing(5);
    14. volLayout->addWidget(volLabel);
    15. volLayout->addWidget(m_volSlider);
    16. volLayout->addWidget(m_volValue);
    To copy to clipboard, switch view to plain text mode 

    finally I connect the value of the last Label with the slider:
    Qt Code:
    1. connect(m_volSlider, SIGNAL( valueChanged(int) ), m_volValue, SLOT(setNum(int) ) );
    To copy to clipboard, switch view to plain text mode 

    The problem is that when the value of the volume label change it modify the size of the widget because it's size change too.
    How can I fix it?
    I set the size policy of the widget containing the layout to fixed without success so:

    Qt Code:
    1. PanelInfo::PanelInfo(QWidget* parent /* = 0 */ )
    2. : QFrame(parent),
    3. m_sound(0)
    4. {
    5. setFrameStyle( QFrame::Box | QFrame::Raised );
    6. setMinimumSize( QSize(135, 240) );
    7. setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
    8. ...more code..
    To copy to clipboard, switch view to plain text mode 

    I hope to get help as I tried to change all.
    Best regards,
    Franco
    Last edited by franco.amato; 4th March 2010 at 02:29.
    Franco Amato

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems addind a simple horizontal layout


  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems addind a simple horizontal layout

    Quote Originally Posted by Lykurg View Post
    Hi the size of the widget change when the value of the label change from 1 to 2 digits ( for example from 9 to 10 ) and from 2 to 3 digits ( for example from 99 to 100 ) and viceversa.
    The minimum value is 0 and the maximum is 100.

    Regards,
    Franco
    Franco Amato

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems addind a simple horizontal layout

    Quote Originally Posted by franco.amato View Post
    Hi the size of the widget change when the value of the label change from 1 to 2 digits ( for example from 9 to 10 ) and from 2 to 3 digits ( for example from 99 to 100 ) and viceversa.
    Yes and therefore use QWidget::setFixedSize() for the label with a proper size that there is enough space for 3 digits. Then the label wont resize ==> the frame/layout neither.

  9. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems addind a simple horizontal layout

    Quote Originally Posted by Lykurg View Post
    Yes and therefore use QWidget::setFixedSize() for the label with a proper size that there is enough space for 3 digits. Then the label wont resize ==> the frame/layout neither.
    Thank you very much.
    I'll try.

    Regards
    Franco Amato

Similar Threads

  1. Qt Image Display...
    By mahe2310 in forum Qt Programming
    Replies: 9
    Last Post: 22nd July 2013, 09:06
  2. How to Display the image in Qt
    By sdastagirmca in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2009, 06:17
  3. How to put custom handle image in QSlider using code?
    By montylee in forum Qt Programming
    Replies: 6
    Last Post: 29th January 2009, 19:38
  4. QSlider custom handle image not displayed
    By planglois in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2008, 13:49
  5. Image display
    By bmn in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2008, 13:52

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.