PDA

View Full Version : using QListWidget and checkboxs ?



tamnv110
12th July 2011, 04:56
Hello everybody!
I want to create a QString listwidget and which contains a checkbox and qstring on the same row. But I can not pass in the second with a time listwidget be, so I used 2 listwidget to save QString and checkbox. So my interface looks very bad, I want my interface like in the picture below. How do I do?

mcosta
12th July 2011, 08:45
Instead of QListWidget use a QTreeWidget with two columns.
Then use QTreeWidget::setItemWidget() to insert a Check Box



item = new QTreeWidgetItem (ui->treeWidget);
item->setText (0, yourText);
ui->treeWidget->setItemWidget (item, 1, new QCheckBox);

Sukesh
12th July 2011, 09:52
you can subclass QStyledItemDelegate and in re implement paintEvent as


void styledListItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{

QStyleOptionViewItem myOpt = option;
initStyleOption(&myOpt,index);
//for drawing the checkbox
QStyleOptionButton opt;


QRect chkRect(option.rect.right() - option.rect.height(),
option.rect.top(),
option.rect.height(),
option.rect.height());
opt.rect = chkRect;

if(option.state & QStyle::State_Selected)
opt.state = QStyle::State_On;

QApplication::style()->drawControl(QStyle::CE_CheckBox,
&opt, painter);

//adjust the rect for displayed text
int dw = -chkRect.width();
myOpt.rect.adjust(0,0,dw,0);
QStyledItemDelegate::paint(painter, myOpt, index);
}


set the item delegate for the list widget in your code



listWidget = new QListWidget();

listWidget->setItemDelegate(new styledListItemDelegate());

new QListWidgetItem(tr("Oak"), listWidget);

new QListWidgetItem(tr("Fir"), listWidget);

new QListWidgetItem(tr("Pine"), listWidget);



Please note that I have shown code for enabling checkboxes on the left side of the text you need to write code to handle stuff like switching on and off the checked state etc.

Hope this helps

-Sukesh

tamnv110
12th July 2011, 11:19
Thank mcosta, Sukesh ! I have done.

moviemax
12th July 2011, 13:25
The Widget what you want looks for me like a QTableWidget and neither like a QListWidget or QTreeWidget.
Maybe you can use QCheckBox with "openPersistentEditor" ?