Search code examples
c++qtqlistwidgetqlistwidgetitem

Qt connect itemDoubleClicked to self-defined slot


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());
}

Solution

  • 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*