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
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
You will need to use signal and slot to do so. You can read about it here
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.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Hi, you can try something like this.
Create a new signal to form1 withIn form1 constructor connect the button's clicked signal to the btnClicked slot.Qt Code:
signals: private slots: void btnClicked(); void form1::btnClicked() { emit(transfertCompleted("your text"); }To copy to clipboard, switch view to plain text mode
Now in the mainform, after creating form1 add :Qt Code:
To copy to clipboard, switch view to plain text mode
If you donc have to send text from form1 to mainwindow you can avoid using the slot btnClicked() and just connecting both signals directly.
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
Last edited by vanduongbk; 20th June 2013 at 09:17.
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.
hello,i execute as:
in form1,i have a private slot as:
void form1:n_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
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.
this is my code ,please help me:
mainform.h
mainform.cpp#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
form1.h#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.cpp#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
#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:n_pushButton_clicked()
{
//write data
//i want when click it send data and also send text to mainform
emit(transfertCompleted("transfer complete");
}
Some code is missing, where form1 is created and shown? I think you should move the linejust after you create the object, and not on the mainwindow constructor.Qt Code:
To copy to clipboard, switch view to plain text mode
This is my mainwindow.c code and it works, other files are identitcals too yours at the first look.
Qt Code:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <form1.h> 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); form->show(); } { ui->label->clear(); ui->label->setText(text); }To copy to clipboard, switch view to plain text mode
Note : the code tag is not the one you used ;-)
vanduongbk (20th June 2013)
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
Bookmarks