PDA

View Full Version : Subclassed qlistwidget event handling problem



Annihilator
12th February 2010, 19:50
I subclassed qlistwidget
class CustomListWidget : public QListWidget
{
Q_OBJECT
public:
explicit CustomListWidget(QWidget *parent = 0);
signals:
public slots:
public:
int currentItemIndex;
QListWidgetItem *currentItem;
protected:
void currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous );
void itemPressed ( QListWidgetItem * item );
void paintEvent(QPaintEvent *pe);
void currentRowChanged(int currentRow);
void keyPressEvent(QKeyEvent *event);
void mouseMoveEvent(QMouseEvent *event);
};

Add 3 items into one (3 strings)

wrote event handlers:

void CustomListWidget::currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous )
{
}
void CustomListWidget::currentRowChanged(int currentRow)
{
}
void CustomListWidget::itemPressed ( QListWidgetItem * item )
{
}
void CustomListWidget::paintEvent(QPaintEvent *pe)
{
}
void CustomListWidget::keyPressEvent(QKeyEvent *ev)
{
}
void CustomListWidget::mouseMoveEvent(QMouseEvent *ev)
{
}

Handlers for mouseMoveEvent, keyPressEvent and paintEvent work but for itemPressed, currentRowChanged, currentItemChanged do not. What`s the problem?

Lykurg
12th February 2010, 23:16
Handlers for mouseMoveEvent, keyPressEvent and paintEvent work but for itemPressed, currentRowChanged, currentItemChanged do not. What`s the problem?
It's because they are signals and not event handlers! Use QObject::connect() to connect them to a local slot.

Annihilator
13th February 2010, 17:40
I`ve made

connect(this, SIGNAL(itemPressed( QListWidgetItem * item )), SLOT(itemPressed_s ( QListWidgetItem * item )));
defined slot in header file of mywidget

void itemPressed_s ( QListWidgetItem * item );
but still does not work

Lykurg
13th February 2010, 17:45
where is the connect statement and please can you show us your full header file. Have you also recompiled your project, used the Q_OBJECT makro...

Your syntax is fine, so it normally should work.

Annihilator
13th February 2010, 18:07
class CustomListWidget : public QListWidget
{
Q_OBJECT
public:
explicit CustomListWidget(QWidget *parent = 0);

signals:

//void currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous );
//void itemPressed ( QListWidgetItem * item );
public slots:
void itemPressed_s ( QListWidgetItem * item );
void currentItemChanged_s ( QListWidgetItem * current, QListWidgetItem * previous );
public:
int currentItemIndex;
QListWidgetItem *currentItem;

protected:
void paintEvent(QPaintEvent *pe);
void keyPressEvent(QKeyEvent *event);
void mouseMoveEvent(QMouseEvent *event);
};

I`ve tried to make connection in CustomListWidget constructor (my last post)

Also I`ve tried in MainWindow constructore like


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->listWidget, SIGNAL(itemPressed( QListWidgetItem * item )), SLOT(itemPressed_s ( QListWidgetItem * item )));
}
but nothing happened. I always rebuild my project.

p.s.
here is my slot


void CustomListWidget::itemPressed_s ( QListWidgetItem * item )
{

}

Lykurg
13th February 2010, 18:17
Hi,

make the connection in the c-tor of your CustomListWidget. And what exactly is not working for you? try
void CustomListWidget::itemPressed_s ( QListWidgetItem * item )
{
qWarning() << "slot reached: itemPressed_s";
}

Did you see the output?


And as a note: in your mainwindow it has to be:
QObject::connect(ui->listWidget, SIGNAL(itemPressed( QListWidgetItem * item )), ui->listWidget, SLOT(itemPressed_s ( QListWidgetItem * item )));

aamer4yu
13th February 2010, 18:18
You dont have to mention the type name in signal and slot,,,only the type.
This

QObject::connect(ui->listWidget, SIGNAL(itemPressed( QListWidgetItem * item )), SLOT(itemPressed_s ( QListWidgetItem * item )));

should be -

QObject::connect(ui->listWidget, SIGNAL(itemPressed( QListWidgetItem * )), SLOT(itemPressed_s ( QListWidgetItem * )));
Try and see if it works :)

Lykurg
13th February 2010, 18:20
@aamer4u: Damn, you are right! need to go to an optician;)

Annihilator
13th February 2010, 18:29
Ooops. that's it! Thanks, aamer4yu!

aamer4yu
13th February 2010, 18:55
@Lykurg... it happens sometimes to me too....the tiniest details are skipped by mind ,, u are not alone !!

@Annhilator.. welcome :-)