[Solved] Qt signal connect failing



  • I have a QListWidget that is in a QGridLayout within a QWizardPage. The items in the QListWidget are all of the xml files in a directory. I have everything working but for the signals. I missing something critical but simple I'm sure.

    I've looked at the "QListWidget":http://qt-project.org/doc/qt-4.8/qlistwidget.html documentation and at "this":http://stackoverflow.com/questions/15311241/qlistwidget-event-on-item-click and "this":http://stackoverflow.com/questions/7008611/detecting-if-an-item-is-clicked-at-at-some-row-in-a-qlistwidget post, among others. Everything seems to say itemClicked( QListWidgetItem* ) should be what I need but it and others aren't working i.e. my function new_sequence_selected is never getting called. Connect is also never returning a handle for this, always false.

    Most of the signals I've tried are in the code below.

    Also my browseButton is working just fine.

    Additional info: Working in Win7, VS2012, Qt5, Qt VS Add-in 1.2.2

    Thanks in advance for the help!!

    creator.h
    @
    #include <qwizard.h> .
    #include <QtWidgets>
    #include <QHeaderView>

    class Sequence_selectPage : public QWizardPage
    {
    Q_OBJECT

    public: Sequence_selectPage( QWidget *parent = 0 );

    private slots:
    void browse();

    private:
    QListWidget *sequenceList;
    QDir sequences_dir_path;
    QString selected_sequence;
    };
    @

    creator.cpp

    @
    #include "creator.h"

    Sequence_selectPage::Sequence_selectPage( QWidget *parent ) : QWizardPage( parent )
    {
    first_round = true;

    setTitle( tr( "Select an xml" ) );
    // setup path to Sequence
    sequences_dir_path.absolutePath();
    sequences_dir_path.cd( "Sequence" );
    
    //set up the list for the files
    sequenceList = new QListWidget( this );
    //sequenceList->setSelectionMode( QAbstractItemView::SingleSelection );
    QStringList filter;
    filter << "*.xml";
    
    //add xmls to list
    foreach( QString File, sequences_dir_path.entryList( filter, QDir::Files ) )      //(QStringList)"*.xml"
    {
         sequenceList->addItem( File );
    }
    
    if( sequenceList->count() > 0 )
    {
        sequenceList->item( 0 )->setSelected( true );
    }
    
    browseButton = buttonBuilder( tr( "Browse..." ), this, SLOT( browse() ) );
    
        /*********** Figure out this signal **********/
    connect( sequenceList, SIGNAL( sequenceList->itemClicked( QListWidgetItem* i ) ), this, SLOT( new_sequence_selected( QListWidgetItem* i) ) );
    //connect( sequenceList, SIGNAL( itemClicked( QListWidgetItem* ) ), this, SLOT( new_sequence_selected( QListWidgetItem* ) ) );
    //connect( sequenceList, SIGNAL( itemActivated( QListWidgetItem* ) ), this, SLOT( new_sequence_selected( QListWidgetItem* ) ) );
    //connect( sequenceList, SIGNAL( itemEntered( QListWidgetItem* ) ), this, SLOT( new_sequence_selected( QListWidgetItem* ) ) );
    //connect( sequenceList, SIGNAL( itemPressed( QListWidgetItem* ) ), this, SLOT( new_sequence_selected( QListWidgetItem* ) ) );
    //connect( sequenceList, SIGNAL( itemSelectionChanged( ) ), this, SLOT( new_sequence_selected( ) ) );
    //connect( sequenceList, SIGNAL( itemDoubleClicked( QListWidgetItem* ) ), this, SLOT( new_sequence_selected( QListWidgetItem* ) ) );
    //connect( sequenceList, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( new_sequence_selected( QListWidgetItem* ) ) );
    //connect( sequenceList, SIGNAL( currentItemChanged( QListWidgetItem*, QListWidgetItem* ) ), this, SLOT( new_sequence_selected( QListWidgetItem* ) ) );
    
    selectpageLayout = new QGridLayout( this );
    selectpageLayout->addWidget( sequenceList, 0, 0, 1, 2 );
    selectpageLayout->addWidget( browseButton, 1, 1 );
    
    setLayout( selectpageLayout );
    

    }

    void Sequence_selectPage::new_sequence_selected( QListWidgetItem* sequence )
    {
    selected_sequence = sequence->text();
    }
    @

    buttonBuilder code
    @
    QPushButton *buttonBuilder( QString &button_name, QObject *receiver, const char *member )
    {
    QPushButton *mybutton = new QPushButton( button_name );
    QObject::connect( mybutton, SIGNAL( clicked() ), receiver, member );
    return mybutton;
    }@


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Your connect statement is wrong:

    @
    connect(sequenceList, SIGNAL( sequenceList->itemClicked( QListWidgetItem* i ) ), this, SLOT( new_sequence_selected( QListWidgetItem* i) ) );@

    would be
    @
    connect(sequenceList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(new_sequence_selected(QListWidgetItem*)));

    Or in this case

    connect(sequenceList, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(new_sequence_selected(QListWidgetItem*)));
    @



  • The problem ultimately being that I hadn't declared new_sequence_selected( QListWidgetItem* i) as a slot. With that done
    @
    connect( sequenceList, SIGNAL( itemClicked(QListWidgetItem* ) ), this, SLOT( new_sequence_selected( QListWidgetItem* ) ) );
    @

    returns a handle as it should.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.