-1

I have the following class that extends QListWidget, but I can't seem to connect the doubleClicked signal to the slot I desire. Here's the code implemented - in VS2012. The idea is to be able to double click a item and edit it. I connect the signal to the slot in the constructor, but the slot is never called when I run it through the debugger.

# .h file
class DisplayFeed :
    public QListWidget
{
    Q_OBJECT
public:
    DisplayFeed(QWidget *parent, Logic *logic, int xpos, int ypos, int width, int height, std::string color);
    ~DisplayFeed(void);
    void setColor(std::string color);
    void refresh(std::vector<Event*> *thingsToInclude);
private:
    Logic* logic;
private slots:
    void editItem(QEventStore *item);
};

Below is the .cpp file. QEventStore extends QListWidgetItem. I placed the MessageBox to test the system as well, in case it was my other code that didn't work.

# .cpp file, only relevant methods included
DisplayFeed::DisplayFeed(QWidget *parent, Logic *logic, int xpos, int ypos, int width, int height, std::string color)
: QListWidget(parent)
{
    this->logic = logic;
    setGeometry(xpos, ypos, width, height);
    setColor(color);
    QObject::connect(this, SIGNAL(itemClicked(QEventStore*)), this, SLOT(editItem(QEventStore*)));
    show();
}

void DisplayFeed::editItem(QEventStore *item){
    QMessageBox::information(this,"Hello!","You clicked \""+item->text()+"\"");
    QEventEditor *editor = new QEventEditor(item->getEvent());
}
Emman
  • 1
  • 2

3 Answers3

0

You forgot the Q_OBJECT macro in your DisplayFeed class. It should be like :

# .h file
class DisplayFeed :
    public QListWidget
{
  Q_OBJECT

public:
    DisplayFeed(QWidget *parent, Logic *logic, int xpos, int ypos, int width, int height, std::string color);
    ~DisplayFeed(void);
    void setColor(std::string color);
    void refresh(std::vector<Event*> *thingsToInclude);
private:
    Logic* logic;
private slots:
    void editItem(QEventStore *item);
};

That's the first thing I noticed and may solve your problem. If not I'll look deeper.

EDIT: Read the first answer here

Community
  • 1
  • 1
Mr.Q
  • 104
  • 3
0

There are several changes to do:

  1. Add Q_OBJECT in the .h of displayFeed class

    class DisplayFeed : public QListWidget
    {
        Q_OBJECT
        ...
    };
    
  2. Change your slot with a public slot and a QListWidgetItem* parameter

    public slots:
        void editItem(QListWidgetItem *item);
    
  3. Connect with the good SIGNAL which have the same parameter that your SLOT

    connect(this,SIGNAL(itemDoubleClicked(QListWidgetItem*)), this,SLOT(editItem(QListWidgetItem*)));
    

This works fine for me, hope it helps you.

Rémi
  • 525
  • 1
  • 15
  • 26
  • Sorry man. This doesn't work. There's no point switching to a QListWidgetItem when I've extended it intentionally for a reason – Emman Oct 14 '14 at 09:45
0

I have found the answer. The problem is that the default signal for itemDoubleClicked emits the QListWidgetItem* and emitting a subclass of that doesn't work. So what I had to do was to go to editItem and get it to dynamic_cast the QListWidgetItem* to a QEventStore*

Emman
  • 1
  • 2