1 Attachment(s)
QComboBox to show Qt::PenStyle
Hello!
I'ld like to create a QComboBox-based class to show exactly the thing found on the image:
Attachment 10026
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
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,
_
Re: QComboBox to show Qt::PenStyle
Thanks anda,
but I'm having quite a trouble to work with the QPainter. I receive:
Code:
QPainter::begin: Paint device returned engine
== 0, type
: 2 QPainter::end: Painter not active, aborted
from this:
Code:
for (int aaa = Qt::SolidLine; aaa < Qt::CustomDashLine; aaa++)
{
pix.fill();
painter.begin(&pix);
painter.setPen((Qt::PenStyle)aaa);
painter.drawLine(2,2,6,6); //temporary values
painter.end();
newComboBox->addItem("",pix);
}
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
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,
_
Re: QComboBox to show Qt::PenStyle
So here is the final work, for now:
Code:
//![1] Begins
drawConfigBar->setOrientation(Qt::Vertical);
drawConfigBar->setMovable(true);
drawConfigBar->setFloatable(true);
//![2]
//Pen Style
newComboBox->setToolTip(newComboBox->tr("Line style"));
newComboBox->setEditable(false);
newComboBox
->setIconSize
(QSize(80,
14));
newComboBox->setMinimumWidth(80);
connect(newComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(slotLineStyleSelected(int)));
for (aaa = Qt::SolidLine; aaa < Qt::CustomDashLine; aaa++)
{
pix.fill(Qt::white);
QPen pen
(brush,
2.5,
(Qt
::PenStyle)aaa
);
painter.setPen(pen);
painter.drawLine(2,7,78,7);
newComboBox
->addItem
(QIcon(pix
),
"");
}
newComboBox->setCurrentIndex((int)Qt::SolidLine - 1);
drawConfigBar->addWidget(newComboBox);
//Pen width
newComboBox->setToolTip(newComboBox->tr("Line width"));
newComboBox->setEditable(false);
newComboBox
->setIconSize
(QSize(80,
14));
newComboBox->setMinimumWidth(80);
connect(newComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(slotLineThicknessSelected(int)));
for (aaa = 1; aaa < 6; aaa++)
{
pix.fill(Qt::white);
QPen pen
(brush,
(double)aaa,Qt
::SolidLine);
painter.setPen(pen);
painter.drawLine(2,7,78,7);
newComboBox
->addItem
(QIcon(pix
),
"");
}
newComboBox->setCurrentIndex(0);
drawConfigBar->addWidget(newComboBox);
Re: QComboBox to show Qt::PenStyle
If you extend your addItem calls to contain the actual value
then you can get the value again in the slot
Code:
void YourClass::slotCurrentIndexChanged(int index)
{
int value = comboBox->itemData(index).toInt();
}
Cheers,
_