There is a working app and I am adding a new language for GUI. Everything works fine but some part of the app are simply not translated. QLinguist detects all of them, and I have added a new translations, but still no result.
Here is the fragment of the code that is not getting translation:
"imagecropwindow_p.h":
#include <QWidget>
class QLabel;
class QPushButton;
class QHBoxLayout;
class QVBoxLayout;
class QFrame;
class CropWindowComponents: public QWidget
{
public:
CropWindowComponents(QWidget *parent = nullptr);
QPushButton *changeBtn;
QPushButton *cropBtn;
QPushButton *continueBtn;
QPushButton *cancelBtn;
};
class HorizontalWindow : public CropWindowComponents
{
Q_OBJECT
public:
HorizontalWindow(QWidget *parent = nullptr);
};
class VerticalWindow : public CropWindowComponents
{
Q_OBJECT
public:
VerticalWindow(QWidget *parent = nullptr);
};
"imagecropwindow_p.cpp":
#include "imagecropwindow_p.h"
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
CropWindowComponents::CropWindowComponents(QWidget *parent) :
QWidget(parent)
{
changeBtn = new QPushButton(tr("Change"), this);
cropBtn = new QPushButton(tr("Crop"), this);
continueBtn = new QPushButton(tr("Continue"), this);
cancelBtn = new QPushButton(tr("Cancel"), this);
}
HorizontalWindow::HorizontalWindow(QWidget *parent) :
CropWindowComponents(parent)
{
QHBoxLayout *btnsLyt = new QHBoxLayout;
btnsLyt->setMargin(0);
btnsLyt->addWidget(changeBtn);
btnsLyt->addWidget(cropBtn);
btnsLyt->addWidget(continueBtn);
btnsLyt->addWidget(cancelBtn);
btnsLyt->addStretch();
setLayout(btnsLyt);
}
VerticalWindow::VerticalWindow(QWidget *parent) :
CropWindowComponents(parent)
{
QVBoxLayout *btnsLyt = new QVBoxLayout;
btnsLyt->setMargin(0);
btnsLyt->addWidget(changeBtn);
btnsLyt->addWidget(cropBtn);
btnsLyt->addWidget(continueBtn);
btnsLyt->addWidget(cancelBtn);
btnsLyt->addStretch();
setLayout(btnsLyt);
}
"imagecropperwindow.h":
#include "imagecropwindow_p.h"
class ImageCropperWindow : public QWidget
{
Q_OBJECT
public:
explicit ImageCropperWindow(QWidget *parent = nullptr);
private slots:
void changeWindowOrientation();
private:
HorizontalWindow *horizWindow;
VerticalWindow *verticalWindow;
};
"imagecropperwindow.cpp":
#include "imagecropperwindow.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
ImageCropperWindow::ImageCropperWindow(QWidget *parent) : QWidget(parent)
{
horizWindow = new HorizontalWindow(this);
verticalWindow = new VerticalWindow(this);
connect(horizWindow->changeBtn, &QPushButton::clicked,
this, &ImageCropperWindow::changeWindowOrientation);
connect(verticalWindow->changeBtn, &QPushButton::clicked,
this, &ImageCropperWindow::changeWindowOrientation);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(3);
layout->addWidget(horizWindow);
layout->addWidget(verticalWindow);
verticalWindow->setVisible(false);
setLayout(layout);
}
void ImageCropperWindow::changeWindowOrientation()
{
if (horizWindow->isVisible()) {
horizWindow->setVisible(false);
verticalWindow->setVisible(true);
}
else {
verticalWindow->setVisible(false);
horizWindow->setVisible(true);
}
this->resize(this->minimumSizeHint());
}
And "main.cpp":
#include <QApplication>
#include <QDebug>
#include <QTranslator>
#include "imagecropperwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator newLang;
newLang.load(":/translations/newLanguage.qm");
a.installTranslator(&newLang);
ImageCropperWindow w;
w.show();
return a.exec();
}
You can find the whole project here.
PS: For an example case, I added stars (*) as a new translation, such as
nativeLanuage - ***newLanguage***
Change - ***Change***
Crop - ***Crop***
Continue - ***Continue***
Cancel - ***Cancel***
QTranslator uses the MOC to do the translations so if you want that if you want your widget to be translated you should use the macro Q_OBJECT, in your case CropWindowComponents does not have it, so the solution is to add it:
imagecropwindow_p.h
class CropWindowComponents: public QWidget
{
Q_OBJECT # <--- add this
public:
...
On the other hand do not add the .ts to the .qrc since the .ts only serves to convert it to the .qm binary. When you add a file to the .qrc it is compiled and added to the executable, increasing the size of the last one. Therefore adding the .ts increases the size of the executable unnecessarily.
Related
Consider following main.cpp (the only file of the whole Qt-project):
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QObject>
#pragma once
class collectedFunctions : public QObject
{
Q_OBJECT
public:
collectedFunctions(QObject* parent = 0) {};
~collectedFunctions() {};
public slots:
void setFunc() {
//Calculate 2+2 and change text of the label accordingly
}
};
#include "main.moc"
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QWidget window;
window.resize(200, 200);
window.setWindowTitle("Test-GUI");
QVBoxLayout layout(&window);
QPushButton button(&window);
button.setText("Test");
button.show();
QLabel *label = new QLabel(&window);
label->setText("Hello");
collectedFunctions testingFuncs;
QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);
layout.addWidget(&button);
layout.addWidget(label);
window.show();
return app.exec();
}
My goal with this code is to create a slot that runs a function which the programmer defines (similar to the pyqt-implementation of the signals and slots system). The code above successfully compiles, not throwing any errors and so far meeting my expectations. Yet as soon I want to manipulate a widget (which in this case is the label), the setfunc-slot does not find the defined label. How can I manipulate a widget using this slot (e.g. using setText() at a label or addItems() at a combobox) or generally have them being recognized in the slot.
Update:
Thanks to the solution of this question, I figured that I could simply use a lambda instead of the Q_OBJECT makro, so I removed the header-code and then changed the connect from the Button to following:
QObject::connect(&button, &QPushButton::clicked, [&label](){
int num = 2 + 2;
string text = "2+2 = ";
text += std::to_string(num);
QString qtext = QString::fromStdString(text);
label->setText(qtext); });
you shouldn't use the Q_OBJECT macro in main.cpp.
The Q_OBJECT macro must appear in the private section of a class
definition that declares its own signals and slots or that uses other
services provided by Qt's meta-object system.
The moc tool reads a C++ header file. If it finds one or more class
declarations that contain the Q_OBJECT macro, it produces a C++ source
file containing the meta-object code for those classes. Among other
things, meta-object code is required for the signals and slots
mechanism, the run-time type information, and the dynamic property
system.
The C++ source file generated by moc must be compiled and linked with
the implementation of the class.
When you have your class defined in .cpp file instead of .h file moc fails to process it properly.
you need a separate file for class:
in collectedfunctions.h
#pragma once
#include <QObject>
class collectedFunctions: public QObject
{
Q_OBJECT
public:
collectedFunctions(QObject *parent = 0);
~collectedFunctions();
public slots:
void setFunc();
};
and in collectedfunctions.cpp
#include "collectedfunctions.h"
#include <QDebug>
collectedFunctions::collectedFunctions(QObject *parent)
{
}
collectedFunctions::~collectedFunctions()
{
}
void collectedFunctions::setFunc()
{
qDebug() << "2+2 = " << 2 + 2;
}
and in your main.cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include "collectedfunctions.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(200, 200);
window.setWindowTitle("Test-GUI");
QVBoxLayout layout(&window);
QPushButton button(&window);
button.setText("Test");
button.show();
QLabel *label = new QLabel(&window);
label->setText("Hello");
collectedFunctions testingFuncs;
QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);
layout.addWidget(&button);
layout.addWidget(label);
window.show();
return app.exec();
}
don't forget to add QT +=core gui widgets in your .pro file.
Result:
If you want to change QLabel Text :
in collectedfunctions.h
#pragma once
#include <QObject>
class collectedFunctions: public QObject
{
Q_OBJECT
public:
explicit collectedFunctions(QObject *parent = 0);
~collectedFunctions();
signals:
void updateLable(int num);
public slots:
void setFunc();
private:
int num = 0;
};
and in collectedfunctions.cpp
#include "collectedfunctions.h"
#include <QDebug>
collectedFunctions::collectedFunctions(QObject *parent)
{
}
collectedFunctions::~collectedFunctions()
{
}
void collectedFunctions::setFunc()
{
qDebug() << "2+2 = " << 2 + 2;
num++;
emit updateLable(num);
}
and in your main.cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include "collectedfunctions.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(200, 200);
window.setWindowTitle("Test-GUI");
QVBoxLayout layout(&window);
QPushButton button(&window);
button.setText("Test");
button.show();
QLabel *label = new QLabel(&window);
label->setText("Hello");
collectedFunctions testingFuncs;
QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);
QObject::connect(&testingFuncs, &collectedFunctions::updateLable, [&label](int num)
{
qDebug() << "Number = " << num;
label->setText(QString::number(num));
});
layout.addWidget(&button);
layout.addWidget(label);
window.show();
return app.exec();
}
you can see:
I'm working on an application where I need to be able to draw a line between two QWidget objects. I have tried quite a few things, but my current attempt (which I think is in the right direction I just think I'm missing something) is to have the containing widget (which I called DrawWidget and which holds the QGridLayout that the QWidget objects are added to) override the paintEvent method and call the QPainter::drawLine() function.
The issues I'm having are that:
No matter how I try to get the position of the widgets, the endpoints of the line are always in the wrong place
Whenever I try to draw a second line, the first line that I drew gets erased.
Here is the paintEvent function of the containing widget:
void paintEvent(QPaintEvent *)
{
if (!drewSinceUpdate){
drewSinceUpdate = true;
QPainter painter(this);
painter.setPen(QPen(Qt::black));
painter.drawLine(start->geometry().center(), end->geometry().center());
}
}
I have tried many different ways to get the correct position of the widgets in the last line of paintEvent, which I will post some of the ways (I can't remember all of them):
painter.drawLine(start->pos(), end->pos());
painter.drawLine(start->mapToGlobal(start->geometry().center()), end->mapToGlobal(end->geometry().center()));
painter.drawLine(this->mapToGlobal(start->geometry().center()), this->mapToGlobal(end->geometry().center()));
painter.drawLine(start->mapTo(this, start->pos()), end->mapTo(this, end->pos()));
painter.drawLine(this->mapFrom(start, start->pos()), this->mapFrom(end, end->pos()));
And just to make my question clear, here is an example of what I am looking for, taken from QT Diagram Scene Example:
But this is what I end up getting:
Thank you for any help you can provide.
NOTE:
-start and end are both QWidget objects which I passed in using another method
-The hierarchy relevant to DrawWidget is:
QMainWindow
->QScrollArea
->DrawWidget
->QGridLayout
->Items <-- These are the things I want to connect
EDIT: To make a Complete and Verifiable example, here is the entirety of the relevant code.
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QScrollBar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Setting up the relevant hierarchy
ui->setupUi(this);
scrollArea = new QScrollArea();
setCentralWidget(scrollArea);
drawWidget = new DrawWidget();
gridLayout = new QGridLayout();
gridLayout->setSpacing(300);
drawWidget->setLayout(gridLayout);
scrollArea->setWidget(drawWidget);
scrollArea->setWidgetResizable(true);
AddItemSlot();
QApplication::connect(scrollArea->horizontalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(scrollHorizontal()));
}
// This is just creating a single one of the example widgets which I want to connect
QWidget* MainWindow::CreateNewItem(){
QWidget* itemWidget = new QWidget();
itemWidget->setStyleSheet("background-color: lightgray");
QHBoxLayout* singleItemLayout = new QHBoxLayout();
itemWidget->setLayout(singleItemLayout);
QTextEdit* textEdit = new QTextEdit(std::to_string(counter++).c_str());
textEdit->setStyleSheet("background-color:white;");
singleItemLayout->addWidget(textEdit);
QVBoxLayout* rightSidePanel = new QVBoxLayout();
rightSidePanel->setAlignment(Qt::AlignTop);
QPushButton* button1 = new QPushButton("Top Button");
QApplication::connect(button1, SIGNAL(clicked(bool)), this, SLOT(AddItemSlot()));
rightSidePanel->addWidget(button1);
QWidget* rightPanelWidget = new QWidget();
rightSidePanel->setMargin(0);
rightPanelWidget->setLayout(rightSidePanel);
singleItemLayout->addWidget(rightPanelWidget);
itemWidget->setLayout(singleItemLayout);
itemWidget->setMinimumWidth(400);
itemWidget->setFixedSize(400,200);
return itemWidget;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::scrollHorizontal()
{
scrollArea->ensureWidgetVisible(noteItems.back());
}
void MainWindow::AddItemSlot()
{
QWidget* w = CreateNewItem();
gridLayout->addWidget(w,currRow, currCol++);
if (!noteItems.empty()){
drawWidget->updateEndpoints(noteItems.back(), w);
}
noteItems.push_back(w);
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGridLayout>
#include <QWidget>
#include <QMainWindow>
#include <QScrollArea>
#include <drawwidget.h>
#include "drawscrollarea.h"
#include <vector>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void scrollHorizontal();
void AddItemSlot();
private:
Ui::MainWindow *ui;
QWidget* CreateNewItem();
int counter = 0, currCol = 0, currRow = 0;
std::vector<QWidget*> noteItems;
QScrollArea* scrollArea;
DrawWidget* drawWidget;
QGridLayout* gridLayout;
};
#endif // MAINWINDOW_H
DrawWidget.cpp:
#include "drawwidget.h"
#include <QDebug>
#include <QRect>
DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{
}
void DrawWidget::paintEvent(QPaintEvent *)
{
if (!drewSinceUpdate){
drewSinceUpdate = true;
QPainter painter(this);
painter.setPen(QPen(Qt::black));
for (ConnectedPair pair : items){
const QWidget* from = pair.from;
const QWidget* to =pair.to;
QPoint start = from->mapToGlobal(from->rect().topRight() + QPoint(0, from->height()/2));
QPoint end = to->mapToGlobal(to->rect().topLeft() + QPoint(0, to->height()/2));
painter.drawLine(mapFromGlobal(start), mapFromGlobal(end));
}
}
}
void DrawWidget::updateEndpoints(QWidget* startIn, QWidget* endIn){
drewSinceUpdate = false;
items.push_back(ConnectedPair{startIn, endIn});
}
DrawWidget.h
#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H
#include <QWidget>
#include <QPainter>
#include <QtCore>
#include <vector>
class DrawWidget : public QWidget
{
Q_OBJECT
public:
explicit DrawWidget(QWidget *parent = nullptr);
void updateEndpoints(QWidget* startIn, QWidget* endIn);
virtual void paintEvent(QPaintEvent *);
signals:
private:
struct ConnectedPair {
const QWidget* from;
const QWidget* to;
};
std::vector<ConnectedPair> items;
bool drewSinceUpdate = true;
};
#endif // DRAWWIDGET_H
For this case we use the function mapToGlobal() and mapfromGlobal(), since pos() returns a position with respect to the parent and this can cause problems if the widget has different parents.
drawwidget.h
#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H
#include <QWidget>
class DrawWidget : public QWidget
{
Q_OBJECT
public:
explicit DrawWidget(QWidget *parent = nullptr);
void addWidgets(const QWidget *from, const QWidget *to);
protected:
void paintEvent(QPaintEvent *);
private:
struct WidgetsConnected {
const QWidget* from;
const QWidget* to;
};
QList<WidgetsConnected> list;
};
#endif // DRAWWIDGET_H
drawwidget.cpp
#include "drawwidget.h"
#include <QPainter>
DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{
}
void DrawWidget::addWidgets(const QWidget * from, const QWidget * to)
{
list.append(WidgetsConnected{from , to});
update();
}
void DrawWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
for(const WidgetsConnected el: list){
const QWidget* from = el.from;
const QWidget* to = el.to;
QPoint start = from->mapToGlobal(from->rect().topRight() + QPoint(0, from->height()/2));
QPoint end = to->mapToGlobal(to->rect().topLeft() + QPoint(0, to->height()/2));
painter.drawLine(mapFromGlobal(start), mapFromGlobal(end));
}
}
The complete example can be found here.
I am learning Qt and i came up with an idea to switch on and off an LED with buttons, i got it working but i couldn't figure out how to implement the same with a toggle button. I will be posting the code below, please take a look and let me know how i can implement the above stated behavior.
pulseledwidget.h
#ifndef PULSINGLEDWIDGET_H
#define PULSINGLEDWIDGET_H
#include <QLabel>
#include <QPixmap>
#include <QTimer>
#include <QObject>
#include <QDebug>
class pulsingLedWidget : public QLabel
{
Q_OBJECT
public:
explicit pulsingLedWidget(QWidget *parent = 0);
public slots:
void startPulsing();
void extinguish();
private:
QPixmap onPixmap,offPixmap;
QTimer timer;
bool state;
};
#endif // PULSINGLEDWIDGET_H
pulseledwidget.cpp
#include "pulsingledwidget.h"
pulsingLedWidget::pulsingLedWidget(QWidget *parent) : QLabel(parent)
{
onPixmap.load(":/ledon.png");
offPixmap.load(":/ledoff.png");
state = true;
setPixmap(offPixmap);
timer.setInterval(200);
connect(&timer,SIGNAL(timeout()),this,SLOT(startPulsing()));
}
void pulsingLedWidget::startPulsing()
{
timer.start();
if(state)
{
setPixmap(onPixmap);
state = false;
}
else
{
setPixmap(offPixmap);
state = true;
}
}
void pulsingLedWidget::extinguish()
{
timer.stop();
setPixmap(offPixmap);
}
leddialog.h
#ifndef LEDDIALOG_H
#define LEDDIALOG_H
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include "pulsingledwidget.h"
class ledDialog : public QDialog
{
Q_OBJECT
public:
explicit ledDialog(QWidget *parent = 0);
private:
QPushButton *onbutton;
QPushButton *offbutton;
QVBoxLayout *layout;
pulsingLedWidget *ledwidget;
};
#endif // LEDDIALOG_H
leddialog.cpp
#include "leddialog.h"
ledDialog::ledDialog(QWidget *parent) : QDialog(parent)
{
QPushButton *onbutton = new QPushButton("On");
QPushButton *offbutton = new QPushButton("Off");
QVBoxLayout *layout = new QVBoxLayout(this);
pulsingLedWidget *ledwidget = new pulsingLedWidget(this);
ledwidget->setAlignment(Qt::AlignCenter);
layout->addWidget(ledwidget);
layout->addWidget(onbutton);
layout->addWidget(offbutton);
resize(150,200);
connect(onbutton,SIGNAL(clicked(bool)),ledwidget,SLOT(startPulsing()));
connect(offbutton,SIGNAL(clicked(bool)),ledwidget,SLOT(extinguish()));
}
What i have tried :
leddialog.h
#ifndef LEDDIALOG_H
#define LEDDIALOG_H
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include "pulsingledwidget.h"
class ledDialog : public QDialog
{
Q_OBJECT
public:
explicit ledDialog(QWidget *parent = 0);
private slots:
void makeDecision(bool state);
private:
QPushButton *onbutton;
QPushButton *offbutton;
QVBoxLayout *layout;
pulsingLedWidget *ledwidget;
};
#endif // LEDDIALOG_H
leddialog.cpp
#include "leddialog.h"
ledDialog::ledDialog(QWidget *parent) : QDialog(parent)
{
QPushButton *onbutton = new QPushButton("On");
QPushButton *offbutton = new QPushButton("Off");
QVBoxLayout *layout = new QVBoxLayout(this);
pulsingLedWidget *ledwidget = new pulsingLedWidget(this);
ledwidget->setAlignment(Qt::AlignCenter);
onbutton->setCheckable(true);
layout->addWidget(ledwidget);
layout->addWidget(onbutton);
layout->addWidget(offbutton);
resize(150,200);
connect(onbutton,SIGNAL(toggled(bool)),this,SLOT(makeDecision(bool)));
}
void ledDialog::makeDecision(bool state)
{
if(state)
{
ledwidget->startPulsing();
}
else
{
ledwidget->extinguish();
}
}
but the above procedure is not working. I tried to run it in debug mode to know the mistake that i have made, it is showing "Segmentation fault" error.
I'm trying to write GUI for a program that must perform some actions with given files. Its logic will be:
1) Program starts with one text field and one button created.
2) If I click on button, I can choose some .exe file. If the file is choosen, its path is set to the textfield that is logicaly linked with first my button.
3) If the file is choosen on previous step, a new pair of text field and button linked to it is created. The size of main window must change dynamically when a new pair is added.
How can I set the path to the file to current textfield?
I need a possibility to edit data in any text field. How to organize interface so that I could separately use QFileDialog with any pair of textfield and button.
I cant figure out how to use signals/slots in this case.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void makeInterface();
private slots:
void openFile();
};
#endif
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
makeInterface();
}
MainWindow::~MainWindow() {}
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("OpenFile"),
QDir::currentPath(),
tr("Executable Files (*.exe)"));
if (!fileName.isNull())
{
qDebug() << fileName;
}
}
void MainWindow::makeInterface()
{
QGridLayout *mainLayout = new QGridLayout;
QLineEdit *fldFilePath = new QLineEdit;
QPushButton *btnOpenFile = new QPushButton("*.exe");
connect(btnOpenFile, SIGNAL(clicked()), this, SLOT(openFile()));
mainLayout->addWidget(fldFilePath, 0, 0);
mainLayout->addWidget(btnOpenFile, 0, 1);
QPushButton *btnBuild = new QPushButton("Build");
mainLayout->addWidget(btnBuild, 5, 0);
setLayout(mainLayout);
}
You should use QSignalMapper for this.
Your code may looks like this:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSignalMapper>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void makeInterface();
private slots:
void openFile(QWidget* widget);
private:
QSignalMapper _mapper;
};
#endif
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
connect(&_mapper, SIGNAL(mapped(QWidget*)), this, SLOT(openFile(QWidget*)));
makeInterface();
}
MainWindow::~MainWindow() {}
void MainWindow::openFile(QWidget* widget)
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("OpenFile"),
QDir::currentPath(),
tr("Executable Files (*.exe)"));
if (!fileName.isNull())
{
static_cast<QLineEdit*>(widget)->setText(fileName);
}
}
void MainWindow::makeInterface()
{
QGridLayout *mainLayout = new QGridLayout;
QLineEdit *fldFilePath = new QLineEdit;
QPushButton *btnOpenFile = new QPushButton("*.exe");
connect(btnOpenFile, SIGNAL(clicked()), &_mapper, SLOT(map()));
_mapper.setMapping(btnOpenFile, fldFilePath);
mainLayout->addWidget(fldFilePath, 0, 0);
mainLayout->addWidget(btnOpenFile, 0, 1);
QPushButton *btnBuild = new QPushButton("Build");
mainLayout->addWidget(btnBuild, 5, 0);
setLayout(mainLayout);
}
I want to update text in a label in a first window from a second window where is a line edit to write some text. This text should be dispaly in first window.
I spend a week for it.
A famous connect doesn't work.
Is somebody who correct below code and explain how connect should work?
I use Qt in version 5.1.1
firstwindow.h
#ifndef FIRSTWINDOW_H
#define FIRSTWINDOW_H
#include <QMainWindow>
#include "secondwindow.h"
namespace Ui {
class Firstwindow;
}
class Firstwindow : public QMainWindow
{
Q_OBJECT
public:
explicit Firstwindow(QWidget *parent = 0);
~Firstwindow();
public slots:
void addEntry();
private slots:
void on_pushButton_clicked();
private:
Ui::Firstwindow *ui;
Secondwindow *asecondwindow;
Secondwindow *absecondwindow;
Secondwindow *abcsecondwindow;
};
#endif // FIRSTWINDOW_H
secondwindow.h
#ifndef SECONDWINDOW_H
#define SECONDWINDOW_H
#include <QDialog>
#include <QtWidgets>
namespace Ui {
class Secondwindow;
}
class Secondwindow : public QDialog
{
Q_OBJECT
public:
explicit Secondwindow(QWidget *parent = 0);
~Secondwindow();
QLineEdit *lineEdit;
private slots:
void on_pushButton_clicked();
private:
Ui::Secondwindow *ui;
QPushButton *pushButton;
};
#endif // SECONDWINDOW_H
main.cpp
#include "firstwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Firstwindow w;
w.show();
return a.exec();
}
firstwindow.cpp
#include "firstwindow.h"
#include "ui_firstwindow.h"
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
Firstwindow::Firstwindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Firstwindow)
{
ui->setupUi(this);
asecondwindow = new Secondwindow();
QObject::connect(asecondwindow->lineEdit,SIGNAL(textChanged()),this, SLOT(addEntry()));
}
Firstwindow::~Firstwindow()
{
delete ui;
delete asecondwindow;
delete absecondwindow;
delete abcsecondwindow;
}
void Firstwindow::on_pushButton_clicked()
{
absecondwindow = new Secondwindow;
absecondwindow->exec();
}
void Firstwindow::addEntry()
{
abcsecondwindow = new Secondwindow;
if (abcsecondwindow->exec()) {
QString name = abcsecondwindow->lineEdit->text();
ui->label->setText(name);
}
}
secondwindow.cpp
#include "secondwindow.h"
#include "ui_secondwindow.h"
#include <QDialog>
Secondwindow::Secondwindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::Secondwindow)
{
ui->setupUi(this);
}
Secondwindow::~Secondwindow()
{
delete ui;
}
void Secondwindow::on_pushButton_clicked()
{
// emit ui->lineEdit->textChanged();
QDialog::accept();
}
I see the following issues:
QLineEdit does not have a signal textChanged(). It should be textChanged(const QString &) instead. So you have to install your connection like:
QObject::connect(asecondwindow->lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(addEntry(const QString &)));
Please note that I changed the Firstwindow::addEntry() slot to Firstwindow::addEntry(const QString &) to match the signal's signature.
I cannot find when and where your QLineEdit member variable of the Secondwindow class is created.
There is a fundamental design problem with what you're doing. There's no need to expose the second window's internal properties to the first window. Just listen for changes within the second window and emit a signal whenever it changes. Then the first window can just listen to the changes on the second window.
Here's a full example showing what I mean. main.cpp:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
class SecondWindow : public QDialog {
Q_OBJECT
public:
SecondWindow(QMainWindow *parent = 0) : QDialog(parent) {
QLineEdit *edit = new QLineEdit;
QPushButton *close = new QPushButton(QStringLiteral("close"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(edit);
layout->addWidget(close);
setLayout(layout);
connect(edit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
connect(close, SIGNAL(clicked()), this, SLOT(close()));
}
signals:
void textChanged(const QString &text);
};
class FirstWindow : public QMainWindow {
Q_OBJECT
public:
FirstWindow(QMainWindow *parent = 0) : QMainWindow(parent) {
QWidget *central = new QWidget(this);
QPushButton *button = new QPushButton(QStringLiteral("Open"));
label = new QLabel(QStringLiteral("Output appears here"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button);
layout->addWidget(label);
central->setLayout(layout);
setCentralWidget(central);
connect(button, SIGNAL(clicked()), this, SLOT(createWindow()));
}
private slots:
void createWindow() {
SecondWindow *window = new SecondWindow(this);
connect(window, SIGNAL(textChanged(QString)), this, SLOT(setLabelText(QString)));
window->resize(300, 300);
window->exec();
}
void setLabelText(const QString &text) {
label->setText(text);
}
private:
QLabel *label;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FirstWindow w;
w.resize(400, 400);
w.show();
return a.exec();
}
#include "main.moc"
Not that the SecondWindow listens for changes on the QLineEdit and emits its own signal when that value changes. Then the FirstWindow just connects to that signal and changes its own QLabel whenever it receives the signal.