Search code examples
c++qtsignals-slotsqlineeditqt-signals

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


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.


Solution

  • "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).

    void changeText(const QString &input);
    

    Finally, you either need to specify type:

    QObject::connect(ui->outLineEdit, SIGNAL(textChanged(QString)), mainWindow, SLOT(changeText(QString));
    

    Or, use the new Qt-5 syntax:

    connect(ui->outLineEdit, &QLineEdit::textChanged,
            mainWindow, &MainWindow::changeText);