PDA

View Full Version : signals and slots, newbie problem



testQtowiec
19th June 2014, 19:15
Hello

I got a job to do and I came across the problem of a beginner, I do not know yet how to find the signals and slots.

OK, my problem is that I have declared the two methods in a class and I must conncet with the class Controller using signals and slots. So when you squeeze the button that has to execute method of the class.

Please help and some examples of solutions.

anda_skoa
20th June 2014, 09:28
http://qt-project.org/doc/qt-4.8/signalsandslots.html

Cheers,
_

testQtowiec
20th June 2014, 10:01
Thanks but ...
I have class controller.
I have class Search, where I have slot on_pushButton_clicked()
and i have c++ class Find, where I have method.
Program works that, the main windows start, next I click Search Window and on the Search Window i press pushButton and i would like to start method from class Find.
How connect this 3 things in controller.

anda_skoa
20th June 2014, 11:49
The linked documentation covers:
- declaration of a signal
- declaration of a slot
- connecting a signal to a slot

So what kind of additional information are you looking for?

Cheers,
_

testQtowiec
20th June 2014, 12:04
I get lost in files.

SearchWindow.h


signals:
void searchButtonSelected();

public slots:
void on_pushButton_clicked();


SearchWindow.cpp


void SearchListWindow::on_pushButton_clicked()
{
qDebug() << "Search push button clicked";
emit searchButtonSelected();
}

Controller.h


signals:
void startMethodProcess();

private slots:
void searchButtonSelected();


Controller.cpp


void Controller::setupsearchlistwindow(SearchListWindow * searchWindow)
{
connect( ? );
}


Method.cpp

void Method::process();

How to call method process in connect.

anda_skoa
20th June 2014, 13:30
Does Controller have a pointer to an instance of Method?
Do you want Controller::searchButtonSelected() to call Method::process() or is Method::process() a slot itself?

Cheers,
_

testQtowiec
20th June 2014, 13:48
I want to call Method process when button is press

Controller haven't a pointer to an instance of Method, how make it?

anda_skoa
20th June 2014, 16:31
I want to call Method process when button is press


Yes, but that is not what I was asking.
Is Method::process() a slot, i.e. do you want to connect the signal to it, or is it just a normal method, i.e. do you want to call it inside the slot of Controller?



Controller haven't a pointer to an instance of Method, how make it?

Basic C++: create an instance of Method in Controller or pass a Method object to the Controller object.

Cheers,
_

testQtowiec
20th June 2014, 17:11
It's normal method

anda_skoa
20th June 2014, 17:44
Well then



void Controller::setupsearchlistwindow(SearchListWindow * searchWindow)
{
connect( searchWindow, SIGNAL(searchButtonSelected()), this, SLOT(searchButtonSelected()));
}



void Controller::searchButtonSelected()
{
objectOfMethod->process();
}


Cheers,
_

testQtowiec
21st June 2014, 11:35
Thanks for patience :)

I write this code but doesn' work. I try with qDebug message.

Main.cpp


#include "mainwindow.h"
#include "searchlistwindow.h"
#include <QApplication>
#include "controller.hpp"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
SearchListWindow x;
Controller controller;
controller.setupmainwindows(&w);
controller.setupsearchlistwindow(&x);
w.show();

return a.exec();
}


Controller.cpp


void Controller::setupmainwindows(MainWindow *mainWindow)
{

}
void Controller::setupsearchlistwindow(SearchListWindow * searchWindow)
{
connect(searchWindow, SIGNAL(searchButtonSelected()), this, SLOT(searchButtonSelected()));

}

void Controller::searchButtonSelected()
{
qDebug()<<"Test";
}


and show only text from on_pushButton_clicked


void SearchListWindow::on_pushButton_clicked()
{
emit searchButtonSelected();
qDebug() << "Search push button clicked";
}

anda_skoa
21st June 2014, 12:08
What is the return value of the connect call?

Do you get any runtime warning?

Cheers,
_

testQtowiec
21st June 2014, 12:16
I delete connect in setupmainwindows.

0 warnings

in mainWindow.cpp I create searchWindows


void MainWindow::on_Button()
{
SearchListWindow* searchWindow = new SearchListWindow(this);
searchWindow->show();
}

anda_skoa
21st June 2014, 13:26
Well, that instance of SearchListWindow is not connected, is it?
How do you expect the signal to reach the slot without it being connected?

Cheers,
_

testQtowiec
21st June 2014, 13:30
ok, but I don't know how connect this istance.

anda_skoa
22nd June 2014, 10:00
Well, since you have a method for doing that connect, one way to connect this instance of the dialog would be to pass it to that method.

But since you create an instance of the SearchListWindow in main(), maybe you want to use that in Mainwindow instead of creating another one?

Cheers,
_

testQtowiec
22nd June 2014, 13:34
Yes I want to use instance of SearchListWindows from MainWindow

anda_skoa
22nd June 2014, 15:59
You want to use the ifirst nstance you created in main() inside Mainwindow, or use the second instance you created in Mainwindow::on_button?

Cheers,
_