PDA

View Full Version : Custom Signals



hazardpeter
23rd July 2009, 00:26
Hello, I'm having a big problem with signals, this is it:

I have many QPushButton and a QLineEdit in a widget witch the class name is myclass, their names are cmd_1, cmd_2 and cmd_3, and txt. They are declared in the class declaration like so:



class myclass: public QWidget
{
Q_OBJECT

public:
myclass(QWidget *parent = 0);
~myclass();

private:

QPushButton *cmd_1;
QPushButton *cmd_2;
QPushButton *cmd_3;

QLineEdit *txt;

}

What I really need is that when you click in one of the buttons, its values, 1, 2 or 3, is written in txt widget. I have spend many many hours trying to use connect with custom signals but I just couldn't do it. I have also spend many hour in google and didn't find the answer for my questions about custom signals and emits and that stuff. For me the connect function is starting to bee a big mindf#ck. Can someone please help me understand this or just give a web link to help me.

Thanks for now and sorry for my not so good english.

tjm
23rd July 2009, 00:58
Try this. It worked for me if I understand you correctly.


In Qt designer, go to signals/slots editor.
Right-click cmd_1.
Choose "Go to slot..."
Choose "clicked()". This will create a slot for you.
In the slot, update your LineEdit txt widget. ui->txt->setText("1");
Do the same for the other buttons.




#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

private slots:
void on_cmd_3_clicked();
void on_cmd_2_clicked();
void on_cmd_1_clicked();
void on_pushButton_clicked();
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
qDebug("button was pushed");
}

void MainWindow::on_cmd_1_clicked()
{
ui->txt->setText("1");
}

void MainWindow::on_cmd_2_clicked()
{
ui->txt->setText("2");
}

void MainWindow::on_cmd_3_clicked()
{
ui->txt->setText("3");
}

Lykurg
23rd July 2009, 07:25
Please also notice the Newbie section of this board!

You have to establish a normal signal slot connection:



class myclass: public QWidget
{
Q_OBJECT
public:
myclass(QWidget *parent = 0);
~myclass();

private slots:
void addTextForButton1();

private:
QPushButton *cmd_1;
QPushButton *cmd_2;
QPushButton *cmd_3;
QLineEdit *txt;
}

myclass::myclass()
{
QObject::connect(cmd_1, SIGNAL(clicked()), this, SLOT(addTextForButton1()));
}

void myclass::addTextForButton1()
{
txt->setText(txt->text() + "1");
}


Read this (http://doc.qtsoftware.com/4.5/signalsandslots.html) and you also might want to use QObject::sender().

hazardpeter
23rd July 2009, 09:40
Please also notice the Newbie section of this board!

Oh didn't notice that, thanks for the advice.

And Thank you both for the explanation, very helpful!!:cool:

wysota
23rd July 2009, 10:00
I'd use a QSignalMapper.

faldzip
23rd July 2009, 11:38
you don't have to spend many hours googling - just use Qt Assistant where you can find many informations about Qt classes and mechanisms.

hazardpeter
23rd July 2009, 12:09
I'd use a QSignalMapper.

Thanks, I will explor that, it may be helpful in the future.


you don't have to spend many hours googling - just use Qt Assistant where you can find many informations about Qt classes and mechanisms.

I did used Qt Assistant however I couldn't understand a few thinks. In some other cases it has been very helpful. For example in using keyPressEvent and so.