Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect the signal valueChanged from QLineEdit to a custom slot in Qt

I need to connect the valueChanged signal from QLineEdit to a custom slot programatically. I know how to do the connection by using Qt Designer and doing the connection with graphical interface but I would like to do it programmatically so I can learn more about the Signals and Slots.

This is what I have that doesn't work.

.cpp file

// constructor
connect(myLineEdit, SIGNAL(valueChanged(static QString)), this, SLOT(customSlot()));

void MainWindow::customSlot()
{
    qDebug()<< "Calling Slot";
}

.h file

private slots:
    void customSlot();

What am I missing here?

Thanks

like image 419
fs_tigre Avatar asked Dec 14 '13 16:12

fs_tigre


People also ask

How do I connect my signal to my slot QT?

To connect the signal to the slot, we use QObject::connect(). There are several ways to connect signal and slots. The first is to use function pointers: connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed);

How do I use the signal slot editor Qt Designer?

To begin connecting objects, enter the signals and slots editing mode by opening the Edit menu and selecting Edit Signals/Slots, or by pressing the F4 key. All widgets and layouts on the form can be connected together. However, spacers just provide spacing hints to layouts, so they cannot be connected to other objects.

Can we connect one signal with multiple slots?

If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted. Signals are automatically generated by the moc and must not be implemented in the . cpp file. They can never have return types (i.e.


1 Answers

QLineEdit does not seem to have valueChanged signal, but textChanged (refer to the Qt documentation for complete list of supported signals). You need to change your connect() function call too. It should be:

connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot()));

If you need the handle the new text value in your slot, you can define it as customSlot(const QString &newValue) instead, so your connection will look like:

connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot(const QString &)));
like image 130
vahancho Avatar answered Oct 07 '22 20:10

vahancho