PDA

View Full Version : button clicked to set text in another form



vanduongbk
20th June 2013, 05:17
hello everyone
i have a form1 and mainform,
how when i click a button in form1 and a text was set to Qlabel in mainform
examples as when click ok button in form1 and qlabel in mainform was set text "complete transfer"
thank you

Santosh Reddy
20th June 2013, 06:05
You will need to use signal and slot to do so. You can read about it here (http://qt-project.org/doc/qt-4.8/signalsandslots.html)

Connect the clicked() signal of the button in form1 to a slot of the widget/object managing the mainfrom, and in this slot set the text of the QLabel.

vanduongbk
20th June 2013, 08:10
You will need to use signal and slot to do so. You can read about it here (http://qt-project.org/doc/qt-4.8/signalsandslots.html)

Connect the clicked() signal of the button in form1 to a slot of the widget/object managing the mainfrom, and in this slot set the text of the QLabel.

hello,
i put in mainform.h
public slots:
void update_label1(QString string);

in form1.h ,i have consist a include "mainform.h>
and in form1.c ,i use command connect(ui->pushButton,SIGNAL(clicked()),what's here,SLOT(update_label1("text")));
plz me,thank

nix
20th June 2013, 08:35
Hi, you can try something like this.

Create a new signal to form1 with
signals:
void transfertCompleted(QString);

private slots:
void btnClicked();

void form1::btnClicked()
{
emit(transfertCompleted("your text");
}

In form1 constructor connect the button's clicked signal to the btnClicked slot.

Now in the mainform, after creating form1 add :
connect(form1, SIGNAL(transfertCompleted(QString)), this, SLOT(update_label1(QString)))

If you donc have to send text from form1 to mainwindow you can avoid using the slot btnClicked() and just connecting both signals directly.

vanduongbk
20th June 2013, 09:06
hi nix, i dont know what is emit
and you can a demo code for void transfertCompleted(QString)

in form1,i have a private slot as:
void form1::on_pushButton_clicked()
{
//some a calculate and write a data ro uart here
//i want to write text to mainform
}

i want to do that when i click button ,it will send data to uart and in mainform ,qlabel text is set as "sent complete"
thank

nix
20th June 2013, 09:16
First, you should really read some Qt doc about Signals and Slots, there is link is in a previous message. It's the most important thing in Qt, so be sure it's not a waste a time.

transfertCompleted(QString) is not a method and doesn't have any code : it's a signal.
emit() is a qt fonction that "emit" (or raise) the signal, then Qt will internaly search all slots connected to that signal and execute them.

vanduongbk
20th June 2013, 09:38
hello,i execute as:

in form1,i have a private slot as:
void form1::on_pushButton_clicked()
{
//some a calculate and write a data ro uart here
//i want to write text to mainform
emit(transfertCompleted("your text");
}

in mainform ,i set slot as:
public slots:
void update_label1(const QString &string);

void mainfor::update_label1(const QString &string)
{
ui->label_channel1->clear();
ui->label_channel1->setText(string);
}

and in mainform, connect(form1, SIGNAL(transfertCompleted(QString)), this, SLOT(update_label1(QString)))
when execute ,it not have error but release error as :The program has unexpectedly finished
plz help me

nix
20th June 2013, 09:49
Use Code Tag please, the code is just not readable the way you did.

Can you run in debug mode with the debugger in order to get a backtrace.

Can you post the complete code for those two classes.

vanduongbk
20th June 2013, 10:02
this is my code ,please help me:
mainform.h


#ifndef MAINFORM_H
#define MAINFORM_H

#include <QMainWindow>
#include "form1.h"

namespace Ui {
class mainform;
}

class mainform : public QMainWindow
{
Q_OBJECT

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

public slots:
void update_label(const QString &string);

private:
Ui::mainform *ui;
};

#endif // MAINFORM_H


mainform.cpp


#include "mainform.h"
#include "ui_mainform.h"

mainform::mainform(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::mainform)
{
ui->setupUi(this);
connect(form1, SIGNAL(transfertCompleted(QString)), this, SLOT(update_label(QString)))
}

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

void mainform::update_label(const QString &string)
{
ui->label->clear();
ui->label->setText(string);
}


form1.h


#ifndef FORM1_H
#define FORM1_H

#include <QDialog>

namespace Ui {
class form1;
}

class form1 : public QDialog
{
Q_OBJECT

signals:
void transfertCompleted(QString);
public:
explicit form1(QWidget *parent = 0);
~form1();

private slots:
void on_pushButton_clicked();

private:
Ui::form1 *ui;
};

#endif // FORM1_H

form1.cpp


#include "form1.h"
#include "ui_form1.h"

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

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

void form1::on_pushButton_clicked()
{
//write data
//i want when click it send data and also send text to mainform
emit(transfertCompleted("transfer complete");
}

nix
20th June 2013, 10:27
Some code is missing, where form1 is created and shown? I think you should move the line
connect(form1, SIGNAL(transfertCompleted(QString)), this, SLOT(update_label(QString))) just after you create the object, and not on the mainwindow constructor.

This is my mainwindow.c code and it works, other files are identitcals too yours at the first look.

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

#include <form1.h>

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

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

void MainWindow::openWindow()
{
Form1 *form = new Form1(this);
connect(form, SIGNAL(transfertCompleted(QString)), this, SLOT(updateLabel(QString)));
form->show();
}

void MainWindow::updateLabel(QString text)
{
ui->label->clear();
ui->label->setText(text);
}

Note : the code tag is not the one you used ;-)

vanduongbk
20th June 2013, 10:36
thank you
but my pushbutton is in form1 ,no mainwindow
i only have qlabel with a name is lable in mainwindow
can not use ui->pushButton in mainwindow

nix
20th June 2013, 10:43
No, i'm in the same case. The pushbutton in the mainwindow just shows the second window (form1). The second window has its own pushbutton just like you.
How does you second window is created and shown?

Complete code :
mainwindow.c

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

#include <form1.h>

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

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

void MainWindow::openWindow()
{
Form1 *form = new Form1(this);
connect(form, SIGNAL(transfertCompleted(QString)), this, SLOT(updateLabel(QString)));
form->show();
}

void MainWindow::updateLabel(QString text)
{
ui->label->clear();
ui->label->setText(text);
}


form1.c

#include "form1.h"
#include "ui_form1.h"

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

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

void Form1::on_pushButton_clicked()
{
//write data
//i want when click it send data and also send text to mainform
emit(transfertCompleted("transfer complete"));
}



#ifndef FORM1_H
#define FORM1_H

#include <QDialog>

namespace Ui {
class Form1;
}

class Form1 : public QDialog
{
Q_OBJECT

public:
explicit Form1(QWidget *parent = 0);
~Form1();
signals:
void transfertCompleted(QString);

private:
Ui::Form1 *ui;
private slots:
void on_pushButton_clicked();
};

#endif // FORM1_H



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
Ui::MainWindow *ui;
private slots:
void openWindow();
void updateLabel(QString text);

};

#endif // MAINWINDOW_H

vanduongbk
20th June 2013, 11:07
hi ,i compiler and have error as:
mainscreen.cpp:180: error: C2039: 'pushButton' : is not a member of 'Ui::MainScreen'
how to resolve it

nix
20th June 2013, 11:27
Im my case I use a mainwindow with a pushbutton. A press on that button creates the form1 and shows it. Another click in pushbutton from the form1 change the content of the label in mainwindow. I supposed that it was the way you did it, but it may be not.
Maybe you do not need this behavior.

I still don't get how you create your form1, the problem is probably around here. How does your mainwindow know about form1 in you code???
Are you creating both windows in your main function? In that case move the connect between both windows to the main fonction too.

vanduongbk
21st June 2013, 03:42
hi ,
i creat form1 only with pushButton to send data and i want to do when i click pushbutton in form1 ,it also simultaneously display with content a text that "transfer complete" on Qlabel in mainwindow
plz help me in a demo code

nix
21st June 2013, 07:54
This is quick & dirty, just demo, it is how i understand your problem.
9173

vanduongbk
23rd June 2013, 16:07
hello nix
your code is run ok
but i want to ask how i do not need show form1 ,only click in form1 then back to mainform and a text on mainform is changed without show form1
thank

nix
23rd June 2013, 16:28
but i want to ask how i do not need show form1 ,only click in form1 then back to mainform and a text on mainform is changed without show form1

I'm sorry but I don't understand your problem.
You want click on form1, so it have to be shown. If you just want form1 to not be visible after you have pressed the button and change the text into the mainwindow, just connect the clicked() signal of your form1 button to the close() slot of form1.
But this is probably not what you meant, in that case please try to give a complete description of what you are trying to do, especially when windows are created, shown and hide.