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

Monday, February 27th 2012, 8:17am

How to connect two forms

Hi
I am new to Qt Creator and i m using it on Ubuntu

all i want to know is how to connect two forms for one project.

Suppose the mainwindow.ui form has pushbutton. On button click, how the form.ui(another form) shows.

i have been trying signals n slots n inheritance. but all in vain

please help

Thanking You

2

Monday, February 27th 2012, 5:12pm

First, have you got a simple signal/slot connection to work? That is a good place to start. Show what you have tried for that. e.g. when user pushes a button, write something to console or show a simple message box.

A little more directly for your question: If you want the main window to show the other form, then it must know about the other form (it must have a pointer to an existing instance, or be able to create a new instance).

-mainwindow has a slot and a pushbutton.
-connect pushbutton's clicked() signal to the slot
-in the slot, show() the other form (or make a new one and show it).

Try it and show us your code if you get stuck.
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

Tuesday, February 28th 2012, 5:26am

connecting two forms

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include "QProcess"


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

connect(pushbutton,SIGNAL(clicked()),this,SLOT(on_pushbutton_clicked()));

}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushbutton_clicked()
{
Form f;

f.show();


}




The above code is for mainwindow.cpp

form.ui is the other window, with its form.h as well.. i am declaring the on_pushbutton_clicked in mainwindow.h as in public slots.

and as u had said, the signal and slots are done in mainwindow.ui

but ryt now the error which is coming is :-

/home/asim/abc-build-desktop/../abc/main.cpp:-1: In function ‘int main(int, char**)’:

/home/asim/abc-build-desktop/../abc/main.cpp:9: error: request for member ‘show’ is ambiguous

/usr/include/qt4/QtGui/qwidget.h:487: error: candidates are: void QWidget::show()

/usr/include/qt4/QtGui/qwidget.h:487: error: void QWidget::show()

4

Tuesday, February 28th 2012, 8:13am

several problems here:

0)
No code tags!! Please edit your post and add them so that your code is actually readable.
[code ] [/ code]

1)
pushbutton =new QPushButton; // -(a)
pushbutton=ui->pushbutton; // -(b)

Why make a new button, (a), then immediately ignore it/reassign it (b) - this is a memory leak, and you just made a new button when you already have one!

2)
How can anybody tell you accuratley what is wrong with Form when you have shown ZERO code that describes Form class.

3)
Scope. You need to learn about what happens to stack variables when they go out of scope.
You code,

Source code

1
2
3
4
5
void MainWindow::on_pushbutton_clicked()
{
Form f;
f.show();
}  // <---- f is destroyed here.  You will only see your form for a fraction of a second, if at all.

is incorrect. Please learn how c++ handles stack and heap variables, and their limitations from scope.
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.

This post has been edited 1 times, last edit by "Amleto" (Feb 28th 2012, 10:15am)


5

Tuesday, February 28th 2012, 8:55am

this is my mainwindow.cpp .. i have removed d additional pushbutton statements

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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include "QProcess"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(pushbutton,SIGNAL(clicked()),this,SLOT(on_pushbutton_clicked()));

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushbutton_clicked()
{
   Form f;

   f.show();


}



this is my form.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "form.h"
#include "ui_form.h"

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

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

this is my mainwindow.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
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QProcess>
#include<QPushButton>
#include "form.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow, public Form
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;

private:
QProcess *proc;

QPushButton *pushbutton;

public slots:
void on_pushbutton_clicked();

};

#endif // MAINWINDOW_H


this is the form.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
#ifndef FORM_H
#define FORM_H

#include <QWidget>
#include<QLabel>
#include<QProcess>
#include<mainwindow.h>

namespace Ui {
    class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = 0);
    ~Form();

public:
    QProcess *proc;

private:
    Ui::Form *ui;
};

#endif // FORM_H


Thank you for replying and what should i use then instead of show() so that my 2nd form stays permenantly or till i close it.

I really want this to work . please help.

6

Tuesday, February 28th 2012, 10:18am

show() is correct, but using a stack variable is not.

As I said, you need to learn some basic c++, and this is not the forum for it:
http://stackoverflow.com/questions/79923…-stack-and-heap
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.

Similar threads