Results 1 to 6 of 6

Thread: QComboBox to show Qt::PenStyle

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QComboBox to show Qt::PenStyle

    Hello!


    I'ld like to create a QComboBox-based class to show exactly the thing found on the image:

    combobox.png

    I tried to find a free one on the web, but I couldn't. Essentially, so, how could I put the line styles inside the QComboBox like that?


    Thanks,

    Momergil

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

    Default Re: QComboBox to show Qt::PenStyle

    That is pretty straight forward:

    1) have a loop that iterates over the value range of pen style
    2) in the loop create a QPixmap, fill it white (or whatever background color you'd like)
    3) create a painter on the pixmap, set the pen style, draw a line
    4) add the pixmap as an item on the combobox.

    For (4) I would suggest to additionally set the pen style as the userData, so you can easily retrieve it in the slot reacting to currenIndexChanged()

    Cheers,
    _

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

    Momergil (10th February 2014)

  4. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox to show Qt::PenStyle

    Thanks anda,

    but I'm having quite a trouble to work with the QPainter. I receive:

    Qt Code:
    1. QPainter::begin: Paint device returned engine == 0, type: 2
    2. QPainter::setPen: Painter not active
    3. QPainter::end: Painter not active, aborted
    To copy to clipboard, switch view to plain text mode 

    from this:

    Qt Code:
    1. for (int aaa = Qt::SolidLine; aaa < Qt::CustomDashLine; aaa++)
    2. {
    3. QPixmap pix;
    4. pix.fill();
    5.  
    6. QPainter painter;
    7.  
    8. painter.begin(&pix);
    9. painter.setPen((Qt::PenStyle)aaa);
    10. painter.drawLine(2,2,6,6); //temporary values
    11. painter.end();
    12.  
    13. newComboBox->addItem("",pix);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Notice that not only I'm having problem with setting the QPainter to the pixmap, but also I couldn't find any suitable addItem method to insert the pixmap inside the QComboBox; both of them don't seem to accept that. :x

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

    Default Re: QComboBox to show Qt::PenStyle

    QPixmap's default constructor creates a null pixmap.
    I am pretty sure you want your pixmap to be larger than 0 pixels right?

    And yes, there is no addItem() that takes a QPixmap, however there is one that takes a QIcon and, drumroll, there is QIcon constructor that takes a QPixmap.

    Cheers,
    _

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

    Momergil (11th February 2014)

  7. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox to show Qt::PenStyle

    So here is the final work, for now:

    Qt Code:
    1. //![1] Begins
    2. drawConfigBar = new QToolBar(QToolBar::tr("Draw Configuration Bar"),this);
    3. drawConfigBar->setOrientation(Qt::Vertical);
    4. drawConfigBar->setMovable(true);
    5. drawConfigBar->setFloatable(true);
    6.  
    7. //![2]
    8. QAction* newAction = NULL;
    9. QComboBox* newComboBox = NULL;
    10.  
    11. //Pen Style
    12. newComboBox = new QComboBox(drawConfigBar);
    13. newComboBox->setToolTip(newComboBox->tr("Line style"));
    14. newComboBox->setEditable(false);
    15. newComboBox->setIconSize(QSize(80,14));
    16. newComboBox->setMinimumWidth(80);
    17. connect(newComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(slotLineStyleSelected(int)));
    18.  
    19. for (aaa = Qt::SolidLine; aaa < Qt::CustomDashLine; aaa++)
    20. {
    21. QPixmap pix(80,14);
    22. pix.fill(Qt::white);
    23.  
    24. QBrush brush(Qt::black);
    25. QPen pen(brush,2.5,(Qt::PenStyle)aaa);
    26.  
    27. QPainter painter(&pix);
    28. painter.setPen(pen);
    29. painter.drawLine(2,7,78,7);
    30.  
    31. newComboBox->addItem(QIcon(pix),"");
    32. }
    33.  
    34. newComboBox->setCurrentIndex((int)Qt::SolidLine - 1);
    35.  
    36. drawConfigBar->addWidget(newComboBox);
    37.  
    38. //Pen width
    39. newComboBox = new QComboBox(drawConfigBar);
    40. newComboBox->setToolTip(newComboBox->tr("Line width"));
    41. newComboBox->setEditable(false);
    42. newComboBox->setIconSize(QSize(80,14));
    43. newComboBox->setMinimumWidth(80);
    44. connect(newComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(slotLineThicknessSelected(int)));
    45.  
    46. for (aaa = 1; aaa < 6; aaa++)
    47. {
    48. QPixmap pix(80,14);
    49. pix.fill(Qt::white);
    50.  
    51. QBrush brush(Qt::black);
    52. QPen pen(brush,(double)aaa,Qt::SolidLine);
    53.  
    54. QPainter painter(&pix);
    55. painter.setPen(pen);
    56. painter.drawLine(2,7,78,7);
    57.  
    58. newComboBox->addItem(QIcon(pix),"");
    59. }
    60.  
    61. newComboBox->setCurrentIndex(0);
    62.  
    63. drawConfigBar->addWidget(newComboBox);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QComboBox to show Qt::PenStyle

    If you extend your addItem calls to contain the actual value

    Qt Code:
    1. newComboBox->addItem(QIcon(pix), QString(), QVariant(aaa));
    To copy to clipboard, switch view to plain text mode 
    then you can get the value again in the slot
    Qt Code:
    1. void YourClass::slotCurrentIndexChanged(int index)
    2. {
    3. int value = comboBox->itemData(index).toInt();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. QComboBox
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2014, 15:14
  2. Replies: 4
    Last Post: 18th October 2013, 18:15
  3. Replies: 6
    Last Post: 12th March 2012, 11:06
  4. Show QComboBox list while editing
    By radix07 in forum Qt Programming
    Replies: 0
    Last Post: 14th October 2011, 17:52
  5. Replies: 3
    Last Post: 12th July 2010, 14:12

Tags for this Thread

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.