QT连接不同Windows/ Mirror的信号和插槽
我是任何形式的编程新手,但必须使用QT进行一个项目,以供我的“工程师编程”课程,我们同时学习C ++的基础知识。
我必须在另一个窗口中显示从一个leandit到lenedit的文本。
我有一个从mainwindow打开的用户风格,在此用户风格中,我有一个leandeDit小部件,该小部件将当前选定的用户显示为qString(来自带有.dirname()的QDIR对象)。但是现在,我也必须在Mainwindow中的lededit中显示相同的字符串。
据我阅读的内容,我必须使用以前使用单个.cpp文件中的窗口小部件进行的“连接(...)”来执行此操作,但是现在我需要将UI对象和信号从一个窗口连接到另一个窗口到另一个窗口,并且我在挣扎。
我的想法/我在互联网上可以找到的是:
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);
}
我知道我知道我在做某事插槽的对象错误,但无法弄清楚如何正确执行操作。
如果有人能帮助我,我会非常感谢。
另外:也许还有另一种方法可以在多个窗口上将文本从一个leandit映射到另一个landedit。如果有人能分享一种方法来做到这一点,我将同样感激。是否有一种方法可以以某种方式定义变量“ QDIR Workinguser”?以一种能够在我的所有.cpp文件中访问和覆盖它的方式?
预先感谢您的任何帮助。问候,
亚历山大·M。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不到你有任何“ mainwindow”命名变量的地方,
但是您还提到
mainWindow
是父级,这意味着您可以随时获得参考,例如:您的信号操作器(
changeText(...)
slot)应该将QString
作为参数(而不是QDIR
),这样您就可以处理如何确切处理转换的方法,以防用户在输入段中输入一些随机文本(text-text--编辑)。最后,您要么需要指定类型:
或者,使用新的QT-5语法:
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:Also, your signal-handler (
changeText(...)
slot) should takeQString
as parameter (instead ofQDir
), this way you handle how exactly the conversion is handled, in case users type some random text in input-field (text-edit).Finally, you either need to specify type:
Or, use the new Qt-5 syntax:
您可以将新信号(与leneDit的文本变成较量相同的参数相同),该信号连接到LineDit的文本变换信号。然后将该信号用户窗连接到MainWindow的插槽。
然后在ontextchanged中写下相同的文本到mainwindow的lineedit
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.
Then in onTextChanged write the same text to mainWindow's lineEdit