PDA

View Full Version : QComboBox to show Qt::PenStyle



Momergil
9th February 2014, 17:05
Hello!


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

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

anda_skoa
9th February 2014, 18:48
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,
_

Momergil
10th February 2014, 11:12
Thanks anda,

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



QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted


from this:



for (int aaa = Qt::SolidLine; aaa < Qt::CustomDashLine; aaa++)
{
QPixmap pix;
pix.fill();

QPainter painter;

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

anda_skoa
10th February 2014, 14:11
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,
_

Momergil
15th February 2014, 18:15
So here is the final work, for now:



//![1] Begins
drawConfigBar = new QToolBar(QToolBar::tr("Draw Configuration Bar"),this);
drawConfigBar->setOrientation(Qt::Vertical);
drawConfigBar->setMovable(true);
drawConfigBar->setFloatable(true);

//![2]
QAction* newAction = NULL;
QComboBox* newComboBox = NULL;

//Pen Style
newComboBox = new QComboBox(drawConfigBar);
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++)
{
QPixmap pix(80,14);
pix.fill(Qt::white);

QBrush brush(Qt::black);
QPen pen(brush,2.5,(Qt::PenStyle)aaa);

QPainter painter(&pix);
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 = new QComboBox(drawConfigBar);
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++)
{
QPixmap pix(80,14);
pix.fill(Qt::white);

QBrush brush(Qt::black);
QPen pen(brush,(double)aaa,Qt::SolidLine);

QPainter painter(&pix);
painter.setPen(pen);
painter.drawLine(2,7,78,7);

newComboBox->addItem(QIcon(pix),"");
}

newComboBox->setCurrentIndex(0);

drawConfigBar->addWidget(newComboBox);

anda_skoa
16th February 2014, 12:43
If you extend your addItem calls to contain the actual value



newComboBox->addItem(QIcon(pix), QString(), QVariant(aaa));

then you can get the value again in the slot


void YourClass::slotCurrentIndexChanged(int index)
{
int value = comboBox->itemData(index).toInt();
}


Cheers,
_