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

Sunday, December 4th 2011, 3:50pm

QT Slot not being called with connect()

I am trying to create a little application but i am stuck with button presses. They just dont seem to call the slot


homewindow.h:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef HOMEWINDOW_H#define HOMEWINDOW_H



#include <QWidget>



class homeWindow :  public QWidget

{

    Q_OBJECT



public:

    homeWindow();

    ~homeWindow();

    void init();



private slots:

    void on_button_clicked();



private:

    QWidget *aWidget;





};



#endif // HOMEWINDOW_H



homewindow.cpp:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "homewindow.h"#include "loginwindow.h"



#include <QtGui>

#include <qapplication.h>

#include <QDesktopWidget>

#include <QVBoxLayout>

#include <QObject>

#include <iostream>



using namespace std;



homeWindow::homeWindow()

{

}



homeWindow::~homeWindow()

{

}



void homeWindow::init()

{

    aWidget = new QWidget; // create a new QWidget



    QPushButton *button1 = new QPushButton("Log Out", this); 

    QObject::connect(button1,SIGNAL(clicked()),this,SLOT(on_button_clicked())); // does not work



    QHBoxLayout *layout = new QHBoxLayout;

    layout->addWidget(button1);



    aWidget->setLayout(layout);



    aWidget->setGeometry(QRect (20,20,300,700)); // size of window



    QRect qRect(QApplication::desktop()->screenGeometry());

    int iXpos=qRect.width()/2-aWidget->width()/2;

    int iYpos=qRect.height()/2-aWidget->height()/2;

    aWidget->move(iXpos,iYpos);

    aWidget->show();



}



void homeWindow::on_button_clicked()

{

    // dosent get here !

    cout<<"here"; // just for debug

}



any help would be appricated

2

Monday, December 5th 2011, 10:58am

works 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.

3

Monday, December 5th 2011, 5:22pm

this is called after a main window has already been created so this is a separate widget don't know if it helps

in the main window i also have a slot for a button press.

4

Monday, December 5th 2011, 11:26pm

it works. so what is the problem again?
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.

5

Tuesday, December 6th 2011, 7:42am

Well when i press the button it just dosent call the slot for some reason.

here's my project:
dannyH has attached the following file:
  • IMTest.zip (17.4 kB - 1 times downloaded - latest: Dec 9th 2011, 9:04am)

This post has been edited 2 times, last edit by "dannyH" (Dec 6th 2011, 3:55pm)


6

Tuesday, December 6th 2011, 6:15pm

I tried your code above and it does call the slot. Either you are looking for something that it doesn't do, or your debugger is in error. what debugger are you using?
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.

7

Wednesday, December 7th 2011, 7:49am

MinGW 4.4 (QT SDK)

8

Wednesday, December 7th 2011, 12:26pm

What are you doing to determine that the slot isn't called? When I try it, the text appears in the qt debugger output window.
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.

9

Wednesday, December 7th 2011, 4:06pm

It dosent appear in mine for some reason

10

Wednesday, December 7th 2011, 6:42pm

for pete's sake! what does 'it doesn't appear' mean. please be explicit.

instead of just looking for some inocuous text, have you tried showing a plain widget instead? It would be much more obvious...
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.

11

Wednesday, December 7th 2011, 6:50pm

It dosent appear means that the text does not appear in the debugger window

what do you mean by plain widget?

12

Wednesday, December 7th 2011, 11:02pm

Source code

1
2
3
4
5
void myslot()
{
QWidget* w = new QWidget();
w->show();
}


if you slot is invoked, you will have an empty widget - ie a new window show.
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.

13

Thursday, December 8th 2011, 7:54am

A new window is not created when i press the button, all that happens is it the login screen is shown again as soon as the home window is shown

14

Thursday, December 8th 2011, 12:34pm

show your latest code.
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.

15

Thursday, December 8th 2011, 3:46pm

here you go
dannyH has attached the following file:
  • IMTest.zip (17.37 kB - 5 times downloaded - latest: Dec 13th 2011, 10:40am)

16

Thursday, December 8th 2011, 9:34pm

I changed main to look like this:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <QtGui/QApplication>
#include "loginwindow.h"
#include "homeWindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //MainWindow w;
    //w.show();
    homeWindow hw;
    hw.init();
    hw.show();

    return a.exec();
}


it does work. clicking on 'log out' opens a new window. clearly you have misunderstood something fundamental, or your tools just aren't working.
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.

17

Friday, December 9th 2011, 7:49am

It works when it is like that but not when the main window has been called before it.

18

Friday, December 9th 2011, 8:03am

I'm going to rebuild the whole application without using the ui files because that seems to be the problem for me.

Cheers for helping me find the problem

This post has been edited 1 times, last edit by "dannyH" (Dec 9th 2011, 8:09am)


19

Friday, December 9th 2011, 4:17pm

ok, well I've just proved that the slot is working, so you are doing something else wrong... I suggest you get the debugger out!
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.

20

Friday, December 9th 2011, 4:24pm

what do you think this does?


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
void MainWindow::on_pushButton_clicked()
{
    QString userTry = ui->usernameEdit->text();
    QString passTry = ui->passwordEdit->text();


    hide();

    homeWindow w;
    w.init();

    show();
}
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.