简体   繁体   中英

QT4 / C++ : no such signals problem

I have a small program to show devices and capture any packets, with GUI.I use QT Designer and Netbeans 6.9 to draw the GUI,but the problem comes when i try to implement a signal/slot.Basically when a combo box is selected, a QlineEdit would show a MAC address of the selected device. The error:

 Object::connect: No such signal QComboBox::selectedDev(int) in MainGUI.cpp:21
Object::connect:  (sender name:   'comboBox')
Object::connect:  (receiver name: 'MYMACBOX')

MainGUI.h

#ifndef _MAINGUI_H
#define    _MAINGUI_H

#include "ui_MainGUI.h"

class MainGUI : public QDialog {
    Q_OBJECT
public:
    MainGUI();
    virtual ~MainGUI();
    void displayDevices();
    void selectedValue();
public slots:
    void showmac(int);

    signals:
    void selectedDev(int);
private:
    Ui::MainGUI widget;
};

MainGUI.cpp

#include "MainGUI.h"
#include "pcapCapture.h"
#include <pcap.h>
#include <iostream>
MainGUI::MainGUI() // constructor
{
    widget.setupUi(this);
    //show devices here    
  QObject::connect(widget.comboBox,SIGNAL(selectedDev(int)),widget.MYMACBOX,SLOT(showmac(int)));
}
void MainGUI::showmac(int value)
{
   //show MAC address here
}

I don't know whts the problem here, i tried different ways to get around this problem, but they won't work.Apologies for any obvious mistakes here, i am still new to QT4( and libpcap).

QComboBox does not have a selectedDev(int) signal. The documentation provides a handy list of signals and slots for every Qt class: QComboBox documentation

You have defined the selectedDev(int) signal in your MainGUI class so your call to connect should be like this: (the arguments to connect are: signal source, signal, slot or signal source, slot or signal.)

QObject::connect(this,SIGNAL(selectedDev(int)),widget.MYMACBOX,SLOT(showmac(int)));

But this won't have any effect as there's nothing firing the selectedDev(int) signal.

Perhaps you could try to connect the combo box's currentIndexChanged(int) to your selectedDev(int) signal, like this:

QObject::connect(widget.comboBox,SIGNAL(currentIndexChanged(int)),this ,SIGNAL(selectedDev(int)));

What I'm doing here is firing the selectedDev(int) signal of your MainGUI when the combo box index changes.

If you simply want to execute showmac(int) when the user select something in the combo box you can't do it more straightforward than this:

QObject::connect(widget.comboBox,SIGNAL(currentIndexChanged(int)),widget.MYMACBOX,SLOT(showmac(int)));

As it seams that you are a bit confused about signals & slots here are some links that might help:

Signals & Slots from the Qt documentation

An introductory Qt article from my blog that explains how a very simple Qt application works (it has nothing to do with it being my blog but it's a nice overview of a Qt application for anyone starting with Qt)

i agree with Raphael above.Qcombobox already has the signal to indicate that the current selection has changed. This is a list of signals emited by qcombobox

Signals
void    activated ( int index )
void    activated ( const QString & text )
void    currentIndexChanged ( int index )
void    currentIndexChanged ( const QString & text )
void    editTextChanged ( const QString & text )
void    highlighted ( int index )
void    highlighted ( const QString & text )

The one you need to connect to your qlineedit is void currentIndexChanged ( int index ) and dont need to implement a new signal.

About the error

The error you are getting is true, as you see from the list of signals emitted by qcombobox above, none of them is QComboBox::selectedDev(int) which makes the error make sense.

Object::connect: No such signal QComboBox::selectedDev(int) in MainGUI.cpp:21

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM