PDA

View Full Version : QcomboBox item height



Beppe
12th June 2009, 09:58
Hi all,

I would like to set with stylesheet the item's height when combo's pop up opens.

any suggestion?

thank you

Beppe

aamer4yu
12th June 2009, 11:32
It seems not possible with style sheet...
I tried -

QComboBox QAbstractItemView
{
font: 75 48pt "Times New Roman";
}

but it doesnt work

spirit
12th June 2009, 11:52
try to set an item size throught a model


#include <QtGui>
#include <QApplication>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QComboBox cb;
cb.addItems(QStringList() << "a" << "b" << "c" << "d");
cb.model()->setData(cb.model()->index(0, 0), QSize(100, 100), Qt::SizeHintRole);
cb.show();
return app.exec();
}

Beppe
15th June 2009, 14:49
Thank you spirit, it works.

I have another problem. I would like to change the scrollbar of the items' list. I have to use the function


void QComboBox::setView ( QAbstractItemView * itemView )

and set my scrollbar for QAbstractItemView using


setVerticalScrollBar ( QScrollBar * )

???

Am i rught ?

spirit
15th June 2009, 15:19
you can get a view using QComboBox::view and then set a new scroll bar.

Beppe
16th June 2009, 08:54
Thanks again spirit, it works, but my scrollbar is not correctly displayed in the combobox's pop up (see scrollbarComboBox.JPG). When I add my scrollbar to a scrollarea it is correctly displayed (see scrollbarScrollArea.JPG).

my combo box code is

mycombobox.cpp


#include "mycombobox.h"


myComboBox::myComboBox(QWidget * parent)
: QComboBox(parent)
{
comboScrollBar = new myscrollbar(0);


this->view()->setVerticalScrollBar(comboScrollBar);


this->setStyleSheet(" \
QComboBox { border: 1px solid gray;\
border-radius: 3px;\
padding: 1px 18px 1px 3px; \
min-width: 6em; \
} \
\
QComboBox:editable {\
background: white;\
}\
\
QComboBox:!editable, QComboBox::drop-down:editable {\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,\
stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);\
}\
\
\
QComboBox:!editable:on, QComboBox::drop-down:editable:on {\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #D3D3D3, stop: 0.4 #D8D8D8,\
stop: 0.5 #DDDDDD, stop: 1.0 #E1E1E1);\
}\
QComboBox:on {\
padding-top: 3px;\
padding-left: 4px;\
}\
QComboBox::drop-down {\
subcontrol-origin: padding;\
subcontrol-position: top right;\
width: 54px;\
border-left-width: 1px;\
border-left-color: darkgray;\
border-left-style: solid; \
border-top-right-radius: 3px; \
border-bottom-right-radius: 3px;\
}\
QComboBox::down-arrow {\
image: url(b_droplist.png);\
}\
\
QComboBox::down-arrow:on { \
top: 1px;\
left: 1px;\
}\
QComboBox QAbstractItemView {\
border: 2px solid darkgray;\
selection-background-color: lightgray;\
}\
");
}

mycombobox.h


#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H

#include <QtCore>
#include <QtGui>
#include <QComboBox>

#include "myscrollbar.h"


class myComboBox : public QComboBox
{
Q_OBJECT
Q_DISABLE_COPY(myComboBox)
public:
myComboBox(QWidget * parent = 0);

private:
myscrollbar *comboScrollBar;
};


#endif // MYCOMBOBOX_H


and my scrollbar code is

myscrollbar.cpp




myscrollbar::myscrollbar(QWidget * parent)
: QScrollBar(Qt::Vertical, parent)
{


this->setStyleSheet(" \
QScrollBar::vertical {\
background:lightgray;\
border-color:black;\
border-style:solid;\
width: 15px;\
margin-top:1px;\
margin-bottom:1px;\
}\
\
QScrollBar::add-page:vertical, \
QScrollBar::sub-page:vertical {\
background: none;\
}\
\
QScrollBar::add-line:vertical{\
background:none;\
} \
\
QScrollBar::sub-line:vertical { \
background:none;\
} \
\
QScrollBar::handle:vertical {\
background: url(slider.png);\
\
border-color:black;\
border-style:solid;\
border-width:1px;\
} \
\
QScrollBar::add-line:pressed,\
QScrollBar::sub-line:pressed,\
QScrollBar::handle:pressed{\
background:lightgray;\
background: url(slider.png);\
}\
");
}

QSize myscrollbar::sizeHint() const
{
return QSize(50,96);
}

QSize myscrollbar::minimumSizeHint() const
{
return QSize(50,96);
}


myscrollbar.h



#ifndef MYSCROLLBAR_H
#define MYSCROLLBAR_H

#include <QtCore>
#include <QtGui>
#include <QScrollBar>

class myscrollbar : public QScrollBar
{
Q_OBJECT
Q_DISABLE_COPY(myscrollbar)
public:
myscrollbar(QWidget * parent = 0);

QSize sizeHint() const;
QSize minimumSizeHint() const;
};

#endif // MYSCROLLBAR_H



have you any idea of the problem ?

spirit
16th June 2009, 10:53
why do you create own scroll bar? you just can set a style sheet for already created scrollbar in a combobox, e.g.


...
cb->view()->setStyleSheet(...);
...

or


...
cb->view()->verticalScrollBar()->setStyleSheet(...);
...

or I missed something?

Beppe
16th June 2009, 11:25
I'm not expert, so you may be right.

Actually I want to use my scrollbar for other widget such as ScrollArea and I think my code is more flexible if I define my custom scrollbar.

spirit
16th June 2009, 11:28
if you wanna apply your style sheet for all scrollbars in your app then use QApplication::setStyleSheet (http://doc.trolltech.com/4.5/qapplication.html#styleSheet-prop).

Beppe
16th June 2009, 12:31
Thank you for the suggestion