×
Namespaces

Variants
Actions

Archived:QListWidget with checkbox

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.

This code example shows the procedure of adding checkbox to a QListWidget. Multiple items can be checked in a list widget by clicking at any place on item.

Article Metadata
Code ExampleTested with
Devices(s): Symbian emulator,E7
Compatibility
Platform(s): Symbian
Symbian
Article
Keywords: QGridLayout, QListWidget
Created: mind_freak (10 Oct 2011)
Last edited: hamishwillee (11 Oct 2012)

Contents

Function and Functionality

This function helps to set the checkbox on list widget, items in list are checkable and multiple selection is possible.

setFlags(widget->item(i)->flags() |Qt::ItemIsUserCheckable);

Sets the check state of the list item to

setCheckState(Qt::Unchecked);

Code Snippet

.h file

#ifndef
MOBIWIDGET_H
#define MOBIWIDGET_H
#include <QWidget>
#include<QListWidgetItem>
namespace Ui {
class mobiwidget;
}
class mobiwidget : public QWidget
{
Q_OBJECT
public:
explicit mobiwidget(QWidget *parent = 0);
~mobiwidget();
private slots:
void on_pushButton_clicked();
private:
Ui::mobiwidget *ui;
public slots:
void myfun();
void myfunction(QListWidgetItem*);
};
.cpp file
#include"mobiwidget.h"
#include "ui_mobiwidget.h"
mobiwidget::mobiwidget(QWidget *parent) :
 
QWidget(parent),
ui(new Ui::mobiwidget)
{
ui->setupUi(this);
QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(myfun()));
QObject::connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(myfunction(QListWidgetItem*)));
}
mobiwidget::~mobiwidget()
{
delete ui;
}
void mobiwidget::on_pushButton_clicked()
{
ui->listWidget->addItem(ui->lineEdit->text());
}
void mobiwidget::myfun()
{
 
for(int i=0;i<ui->listWidget->count();i++){
ui->listWidget->item(i)->setFlags(ui->listWidget->item(i)->flags() |Qt::ItemIsUserCheckable);
ui->listWidget->item(i)->setCheckState(Qt::Unchecked);
}
 
}
void mobiwidget::myfunction(QListWidgetItem *item){
if(item->checkState())
item->setCheckState(Qt::Unchecked);
else
item->setCheckState(Qt::Checked);
}


Snapshot

Checkableitems.JPG

Source Code

Download here File:Mobisnippet.zip

This page was last modified on 11 October 2012, at 01:14.
63 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×