You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, May 10th 2012, 1:57pm

(solved) non void slot call cause missing slot

Hi,

I have created an example slot

public slots:
void foo(int test);

i call this slot:
QPushButton *createButton = new QPushButton;
createButton->setText(tr("create"));
int t=0;
//connect(createButton, SIGNAL(clicked()),this,SLOT(foo()));
connect(createButton, SIGNAL(clicked()),this,SLOT(foo(t)));

and the slot:
void Window::foo(int test)
//void Window::foo()
{
std::cout << " foo" << std::endl;
}


The programm runs but have this error:
"QObject::connect: No such slot Window::foo(t)"

If i try the commented version it runs fine.

Where is my fault?

This post has been edited 2 times, last edit by "akirahinoshiro" (May 12th 2012, 2:13pm)


Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Thursday, May 10th 2012, 3:56pm

The signal click() only has two forms: click() and click(bool) per QAbstractButton class for QPushButton, no int.

3

Thursday, May 10th 2012, 4:29pm

I thought i call the funtion foo(int) so it does not belong to the click(), this is just for the action to call foo.
Is there any other method to call a function when the action click appears?

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

4

Thursday, May 10th 2012, 5:18pm

I'm not sure what you mean? If you want to call a function with parameters you can within the foo(){ foo( 1 ); } // assuming overload of function foo here

5

Thursday, May 10th 2012, 5:36pm

you can't connect signals by using variables in SLOT/SIGNAL, you can only use types


wrong
connect(createButton, SIGNAL(clicked()),this,SLOT(foo(t)));

right*
connect(createButton, SIGNAL(clicked()),this,SLOT(foo(int)));

* BUT you can only connect signals to slots with equal or fewer arguments, so the above example is ALSO wrong.



look at QSignalMapper for getting around this in some circumstances.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

6

Thursday, May 10th 2012, 8:24pm

Okay thank you a lot i got this totally wrong.

A last question for the moment.
What is the right way to get the value entered in the lineEdit and dateEdit in foo(), so i am possible to convert it to std::string and do something with other c++ libs?
The source is attached. (sorry for the extension)
And is there any bad style i should watch in my code?

PS:Yes i know there is some overhead from trying around with QT.
akirahinoshiro has attached the following files:

7

Thursday, May 10th 2012, 11:13pm

connect clicked() to a custom slot () e.g. void myslot();
make a custom signal e.g. mysignal(const std::string&).

connect any slot that takes a std::string to your 'mysignal'

in myslot:

Source code

1
2
3
4
5
6
void myclass::myslot()
{
  QString text = m_lineedit->text();

 emit mysignal(text.toStdString()); // mysignal signature is void mysignal(const std::string&)
}
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

8

Friday, May 11th 2012, 12:27pm

Sorry but i don't get it. I looked at an example from the examples and demos and tried to change it the way you wrote.
Now it compiles without errors but after clicking the button the program crashs.

Could you give me the hint what i made wrong or changing the attached source?
akirahinoshiro has attached the following file:

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

9

Friday, May 11th 2012, 4:40pm

You basically declare the pointers in the header and did not allocate (new) to it. You essentially create a separate instance - and when the function ended coming out of the body, they were destroyed. Compare attached to get a better understanding of this.

unzips in kendrion1 dir.
Junior has attached the following file:
  • kendrion1.zip (1.9 kB - 1 times downloaded - latest: May 11th 2012, 4:54pm)

This post has been edited 1 times, last edit by "Junior" (May 11th 2012, 5:04pm)


10

Friday, May 11th 2012, 5:36pm

Your source also crash while using it.
Was it just a hint what to change or should it run out of the box?

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

11

Friday, May 11th 2012, 6:35pm

Double checked - not crashing here.

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

12

Friday, May 11th 2012, 6:58pm

Here is another cut - check and see if this crashes.
Junior has attached the following file:
  • kendrion2.zip (1.96 kB - 2 times downloaded - latest: May 12th 2012, 1:43pm)

13

Friday, May 11th 2012, 8:24pm

Here is another cut - check and see if this crashes.

that doesn't crash for me.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

14

Saturday, May 12th 2012, 2:11pm

That is great thank you.
I just copied the files over the existing files and then open qtcreator and something goes wrong, in debug mode (i didn't tried before) everything works and in release it crashed.
After creating a new project and then use the files you uploaded everything goes right (release and debug).
So I think I made a mistake not relating to your source.