PDA

View Full Version : How to set event click Custom ListView



jindoniit
9th June 2011, 05:19
I want to set event click of the individual item of the row (only 1 column)?

Santosh Reddy
9th June 2011, 06:54
Please be more descriptive, your post is not clear of what you want.

You want to connect to item click signal?
or
You want to receive the event in you view class, when item is clicked?

jindoniit
9th June 2011, 07:48
let me describe in more detail
Example
I have a custom list and each row has:
One Icon
One Labels
One button
yes, I want to receive the event in my view class, when Icon is clicked

Santosh Reddy
9th June 2011, 08:42
If your having a custom QListView, you can receive the event in

void QListView::mouseReleaseEvent ( QMouseEvent * e )
in there check over which item mouse was released, and it is over the icon area.

jindoniit
9th June 2011, 09:15
i create custom listview as this (http://qt-articles.blogspot.com/2010/07/how-to-customize-listview-in-qt-using.html)

and i write a mouseReleaseEvent but how to assign icon event or function when clicking on it.
I think you understand what I mean.Please guide how to resolve this problem

Santosh Reddy
9th June 2011, 09:26
are you using QStandardItem or cutom Item ?

jindoniit
9th June 2011, 09:34
QStandardItem *item1 = new QStandardItem();
item1->setData(tr("ItemView %1").arg(i+1),ListviewDelegate::headerTextRole);
item1->setData("Some examples the use of graphics effects with canvas items.",ListviewDelegate::subHeaderTextrole);
item1->setData(icon,ListviewDelegate::IconRole1);
item1->setData(label,ListviewDelegate::IconRole2);
item1->setEditable(false);
item1->setSelectable(false);
model->appendRow(item1);


you can see I am using QStandardItem to write Custom listview, in QStandardItem i set data include : icon ,text ,label.
now , i want set 1 function for icon, 1 finction for label

Santosh Reddy
9th June 2011, 21:15
Have a look at this solution

1. Use the QListWidget directly
2. Create a custom QListWidgetItem (say QLWIPushButton)
3. QLWIPushButton is both a QWidget and QListWidgetItem. I have used QWidget so that I can get mouse events.
4. QLWIPushButton will have a QLabel, and QPushButton on it (you can add more if you want)
5. Again one more issue here is, QLabel does not directly support mose clicking (unless used as hyperlink), we have make a custom QLabel (say CustomWidget)


//MainWindow.h
class MainWindow : public QMainWindow
{
...
protected slots:
void buttonClicked(int row);
void labelClicked(int row);
private:
Ui::MainWindow *ui;

QListWidget* listWidget;
QLineEdit* rowLineEdit;
QLineEdit* widgetLineEedit;
};


//MainWindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);

QDockWidget* dock = new QDockWidget("Display Widget");
QWidget* widget = new QWidget(this);
QGridLayout* layout = new QGridLayout(widget);

QLabel* row_label = new QLabel("Row", widget);
QLabel* widget_label = new QLabel("Widget", widget);
rowLineEdit = new QLineEdit("Click a row in List Widget", widget);
widgetLineEedit = new QLineEdit("Click a row in List Widget", widget);

layout->addWidget(row_label, 0 , 0, 1, 1);
layout->addWidget(rowLineEdit, 0 , 1, 1, 1);
layout->addWidget(widget_label, 1 , 0, 1, 1);
layout->addWidget(widgetLineEedit, 1 , 1, 1, 1);

widget->setLayout(layout);
dock->setWidget(widget);
addDockWidget(Qt::RightDockWidgetArea, dock);

// Custom QListWidgetItem in QListWidget
listWidget = new QListWidget(this);
listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 1")));
QLWIPushButton* new_item1 = new QLWIPushButton(QString("Custom QListWidgetItem 1"), listWidget);
listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 2")));
listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 3")));
QLWIPushButton* new_item2 = new QLWIPushButton(QString("Custom QListWidgetItem 2"), listWidget);
listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 4")));

listWidget->setAlternatingRowColors(true);

// Make itemClicked Connection
connect(new_item1, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
connect(new_item1, SIGNAL(labelClicked(int)), this, SLOT(labelClicked(int)));

// Make itemClicked Connection
connect(new_item2, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
connect(new_item2, SIGNAL(labelClicked(int)), this, SLOT(labelClicked(int)));

setCentralWidget(listWidget);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::buttonClicked(int row)
{
rowLineEdit->setText(QString("%1").arg(row));
widgetLineEedit->setText("QPushButton");
}

void MainWindow::labelClicked(int row)
{
rowLineEdit->setText(QString("%1").arg(row));
widgetLineEedit->setText("Custom QLabel");
}


//CustomWidget.h
class CustomWidget : public QLabel
{
Q_OBJECT
public:
explicit CustomWidget(const QString& text, QWidget *parent = 0);

signals:
void released(void);
void clicked(void);

protected:
void mousePressEvent(QMouseEvent* e);
void mouseReleaseEvent(QMouseEvent* e);

private:
bool mousePressed;
};


//CustomWidget.cpp
CustomWidget::CustomWidget(const QString& text, QWidget* parent)
: QLabel(text, parent)
, mousePressed(false)
{
;
}

void CustomWidget::mousePressEvent(QMouseEvent* e)
{
mousePressed = true;
}

void CustomWidget::mouseReleaseEvent(QMouseEvent* e)
{
emit released();
if(mousePressed)
{
emit clicked();
mousePressed = false;
}
}


//QLWIPushButton.h
class QLWIPushButton : public QObject, public QListWidgetItem
{
Q_OBJECT;
public:
explicit QLWIPushButton(const QString text, QListWidget* view);

signals:
void labelClicked(int row);
void buttonClicked(int row);

private slots:
void labelClick(void);
void buttonClick(void);
};


//QLWIPushButton.cpp
QLWIPushButton::QLWIPushButton(const QString text, QListWidget* view)
: QListWidgetItem(view)
{
QWidget* widget = new QWidget(view);
QGridLayout* layout = new QGridLayout(widget);

QPushButton* button = new QPushButton(text, widget);
CustomWidget* Label = new CustomWidget(text, widget);

connect(Label, SIGNAL(released()), this, SLOT(labelClick()));
connect(button, SIGNAL(released()), this, SLOT(buttonClick()));

layout->addWidget(Label, 0, 0);
layout->addWidget(button, 0, 1);

widget->setLayout(layout);

view->setItemWidget(this, widget);
setSizeHint(widget->sizeHint());
}

void QLWIPushButton::labelClick(void)
{
if(listWidget())
emit labelClicked(listWidget()->row(this));
}

void QLWIPushButton::buttonClick(void)
{
if(listWidget())
emit buttonClicked(listWidget()->row(this));
}

jindoniit
10th June 2011, 04:01
Thanks for your reply.
but if i use widget to design ui , i can not custom row widget.
i can not add label ,icon ,button on a row of widget.
for example, i can see image below ,i want design it as below image

Santosh Reddy
10th June 2011, 05:09
It should be possible to create this type of UI using QtDesigner, but you may need to use QTableWidget (instead of QListWidget). You can have three columns (icon column, text column, icon column), and set the icons in QtDesinger using icon resource files. In this case you can connect a your processing slot to
clicked(const QModelIndex &index) signal, which is emitted from QTableWidget.

Still I can't think of a way to add QPushButton to item using QtDesigner:confused:

jindoniit
10th June 2011, 06:28
this is my example. i use QAbstractItemDelegate to create item for Listview, and i also use QsKineticScroller to create scoll listview

Santosh Reddy
10th June 2011, 07:49
Ok, I have gone through example, and am able to run it on my Windows Vista machine (with some modifications to .pro file). It seems to work fine, downloads the icons, and adds to the items etc.

Scrolling works, Clicking on items also work (you are already handling clicks on 30th row)

So, what it that you want do now?

jindoniit
10th June 2011, 08:12
QMouseEvent *me = (QMouseEvent*)event;
if (iconPrevRect.x()<= me->pos().x() && me->pos().x()<=iconPrevRect.x()+iconPrevRect.width())
{
if (index.row()==30)
{
QMessageBox msgBox;
msgBox.setInformativeText("Prev");
msgBox.exec();
}
}

i handled event by: compare position when i click with position of image when i draw it on listview, it is only relatively
I want to assign a event when i click on " prev image", but not based on its location

if you have any way to solve this issue, please help me.

Santosh Reddy
10th June 2011, 11:17
Ok, here it is, have a look at the modifications, and take time to understand the modifications.

6571:cool:

Good Luck.

jindoniit
13th June 2011, 04:06
Thanks
My issue is solved