如何从 Qt 中另一个文件中的函数访问小部件?
我正在开发一个项目,我首先开始在没有 GUI 的情况下制作它,但现在我将其移植到 Qt,但我遇到了问题。
我的“旧”实现位于一个单独的文件中,我尝试从中访问 MainWindow 小部件,以便输出到 QTextBrowser,但我无法这样做。
在 mainwindow.cpp 中,我有这个:
void MainWindow::addString(char* text)
{
std::string input = text;
ui->textBrowser->append(QString::fromStdString(input));
return;
}
在 mainwindow.h 中:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_showWelcome_triggered();
void on_showArithmetic_triggered();
private:
Ui::MainWindow *ui;
public slots:
void btnResolveClicked();
void btnClearClicked();
void hideAll();
void addString(char* output);
};
#endif // MAINWINDOW_H
在 simple_mode.cpp 中:
void test()
{
MainWindow *gui = new MainWindow;
gui->addString("WORKS");
MainWindow:: = gui;
}
但是,这不会将“WORKS”附加到文本浏览器,这是我所需要的,我认为它将它添加到文本浏览器的另一个实例中,该实例不是与主窗口相同。
编辑: 我想做的是将一行文本直接从 simple_mode.cpp 附加到文本浏览器。 顺便说一下,simple_mode 是在没有任何 Qt 辅助的情况下编写的,这就是我使用 std 字符串的原因,目前 textbrowser 小部件充当虚拟终端输出屏幕,而不是像以前那样使用 printf,我想将行附加到 textbrowser 。不过我已经找到了方法,现在不需要了。
我需要帮助
I'm developing a project, and I first began making it without GUI, but now I'm porting it to Qt, but I have a problem.
I have my "old" implementation in a separate file, and I try to access the MainWindow widget from it, in order to output to a QTextBrowser, but I'm not able to do so.
In mainwindow.cpp I have this:
void MainWindow::addString(char* text)
{
std::string input = text;
ui->textBrowser->append(QString::fromStdString(input));
return;
}
In mainwindow.h:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_showWelcome_triggered();
void on_showArithmetic_triggered();
private:
Ui::MainWindow *ui;
public slots:
void btnResolveClicked();
void btnClearClicked();
void hideAll();
void addString(char* output);
};
#endif // MAINWINDOW_H
And in simple_mode.cpp:
void test()
{
MainWindow *gui = new MainWindow;
gui->addString("WORKS");
MainWindow:: = gui;
}
However this doesnt append "WORKS" to the textbrowser, which is what I need, I think it adds it to another instance of text browser which isnt the same as the mainwindow.
EDIT:
What I wanted to do was appending a line of text directly from simple_mode.cpp to the textbrowser.
By the way, simple_mode was written without any Qt aid, that's why I used std strings, and currently the textbrowser widget acts as a virtual terminal output screen, and instead of using printf like I did before, I wanted to append lines to the textbrowser. However I already found my way through, I don't need this now.
I needed help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确实很难说出您想要实现什么,并且代码片段没有涵盖所有可能的错误区域(即
MainWindow
构造函数的定义在哪里?)。另外,格式很糟糕 - 请使用标识和一致的支撑样式。我的建议是简单地在
MainWindow
实例上调用show
。除非您不搞乱MainWindow
构造函数中的ui
初始化,否则此代码片段应该足够了。如果不是 - 向我们提供缺失的代码片段。附带说明一下,您的
addString
方法应如下所示:Return 语句完全没有必要,并且可能会将
text
分配给std::string
导致不必要的内存分配。这并不是世界末日,但对于 C++ 程序员来说,这确实是非常糟糕的做法。It's really hard to tell what do you want to achieve and pieces of code do not cover all possible erroneous areas (i.e. where's the
MainWindow
constructor's definition?). Also, the formatting is terrible - please use idents and consistent bracing style.My advice is simply call
show
onMainWindow
instance. Unless you don't mess upui
initialization inMainWindow
constructor, this snippet should be enough. If it is not - supply us with missing pieces of code.As a side note, your
addString
method should look like this:Return statement is completely unnecessary, and assigning
text
tostd::string
is likely to cause unnecessary memory allocation. It's not like it is the end of the world, but it's really, really bad practice for a C++ programmer.