signals and slots question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi guys.

I have a newbie question that I've been unable to crack. If I have a single
QPopupMenu that is attached to four QPushButtons, how do I extract
information about which button was pressed to a slot connected to a
popupmenu item?

Below is the sample code, which I compile with:

$ moc -o trial_moc.h trial.h
$ g++ -I/usr/lib/qt-3.3/include -o trial trial.cpp \
    -L/usr/lib/qt-3.3/lib -lqt-mt 

I'd like to be able to ascertain which of button1 -> button4 was pressed
inside setCustom, below.

Kind regards,
Angus


$ cat trial.h
#include <qvbox.h>

class MyWidget : public QVBox
{
    Q_OBJECT
public:
    MyWidget( QWidget *parent=0, const char *name=0 );

public slots:
    void setCustom();
};


cat trial.cpp
#include <qapplication.h>
#include <qpushbutton.h>
#include <qpopupmenu.h>

#include <iostream>
#include "trial_moc.h"


MyWidget::MyWidget( QWidget *parent, const char *name )
        : QVBox( parent, name )
{
    QPushButton * button1 = new QPushButton( "1", this, "1" );
    QPushButton * button2 = new QPushButton( "2", this, "2" );
    QPushButton * button3 = new QPushButton( "3", this, "3" );
    QPushButton * button4 = new QPushButton( "4", this, "4" );

    QPopupMenu * pm = new QPopupMenu(this);
    pm->insertItem("&Custom...", this, SLOT(setCustom()), 0, 1);

    button1->setPopup(pm);
    button2->setPopup(pm);
    button3->setPopup(pm);
    button4->setPopup(pm);
}

void MyWidget::setCustom()
{
        std::cout << "setCustom" << std::endl;
}


int main( int argc, char **argv )
{
    QApplication a( argc, argv );

    MyWidget w;
    a.setMainWidget( &w );
    w.show();
    return a.exec();
}


___________________________________________________
.
Account management:  https://mail.kde.org/mailman/listinfo/kde.
Archives: http://lists.kde.org/.
More info: http://www.kde.org/faq.html.

[Index of Archives]     [Trinity (TDE) Desktop Users]     [Fedora KDE]     [Fedora Desktop]     [Linux Kernel]     [Gimp]     [GIMP for Windows]     [Gnome]     [Yosemite Hiking]
  Powered by Linux