Home > Blockchain >  Qt connect signals and slots of different windows/ mirror a lineEdit text on two windows
Qt connect signals and slots of different windows/ mirror a lineEdit text on two windows

Time:07-05

I'm new to any form of programming but have to do a project with Qt for my "programming for engineers" course where we simultaneously learn the basics of c .

I have to display a text from one lineEdit to a lineEdit in another window.

I have a userWindow that opens from the mainWindow and in this userWindow I have a lineEdit widget that displays the current selected user as a QString (from a QDir object with .dirName() ). But now I have to display the same String in a lineEdit in the mainWindow as well.

From what I've read I have to do this with "connect(...)" which I have done before with widgets inside a single .cpp file but now I need to connect a ui object and signal from one window to another and I'm struggling.

My idea /what I could find in the internet was this:

userWindow.cpp

#include "userwindow.h"
#include "ui_userwindow.h"
#include "mainwindow.h"
#include <QDir>
#include <QMessageBox>
#include <QFileDialog>
#include <QFileInfo>


QDir workingUser; //this is the current selected user. I tried defining it in userWindow.h but that wouldn't work how I needed it to but that's a different issue

userWindow::userWindow(QWidget *parent) :               //konstruktor
    QDialog(parent),
    ui(new Ui::userWindow)
{
    ui->setupUi(this);
    QObject::connect(ui->outLineEdit, SIGNAL(textChanged()), mainWindow, SLOT(changeText(workingUser) //I get the error " 'mainWIndow' does not refer to a value "
}

[...]

mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDir>
#include <QMainWindow>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


public slots:
    void changeText(QDir user);   //this is the declaration of my custom SLOT (so the relevant bit)

private slots:
    void on_userButton_clicked();

    void on_settingsButton_clicked();

private:
    Ui::MainWindow *ui;

};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "userwindow.h"
#include "settingswindow.h"
#include "click_test_target.h"
#include "random_number_generator.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   [...]
}
[...]
//here I define the slot
void MainWindow::changeText(QDir user)
{
    QString current = user.dirName();

    ui->userLine->insert("The current working directory is: ");    //"userLine" is the lineEdit I want to write the text to
    ui->userLine->insert(current);
}

I know I'm doing something wrong with the object for the SLOT but can't figure out how to do it correctly.

If anyone could help me I would be very grateful.

Alternatively: perhaps there is another way to mirror the text from one lineEdit to another over multiple windows. If anybody could share a way to do this I would be equally grateful. Is there maybe a way to somehow define the variable "QDir workingUser;" in such a way as to be able to access and overwrite it in all of my .cpp files?

Thank you in advance for any help. Regards,

Alexander M.

CodePudding user response:

"I get the error " 'mainWindow' does not refer to a value"

I don't see you having any "mainWindow" named variable anywhere, but you also mentioned that the MainWindow is the parent, which means you could get reference anytime, like:

MainWindow *mainWindow = qobject_cast<MainWindow *>(this->parent());

Also, your signal-handler (changeText(...) slot) should take QString as parameter (instead of QDir), this way you handle how exactly the conversion is handled, in case users type some random text in input-field (text-edit).

CodePudding user response:

You can create new signal (same params as lineEdit's textChanged) to userWindow which is connected to the textChanged signal of the lineEdit. Then connect that signal userWindow to mainWindow's slot.

//In userWindow.h
signals:
    void textChanged(const QString&);

//In userWindow.cpp
connect(ui.lineEdit, &QLineEdit::textChanged, this, &userWindow::textChanged);

//In mainWindow.cpp
connect(userWindow, &userWindow::textChanged, this, &mainWindow::onTextChanged

Then in onTextChanged write the same text to mainWindow's lineEdit

  • Related