简体   繁体   English

Qt:从类 MainWindow 访问函数:其他文件中的公共 QMainWindow

[英]Qt: Accessing function from class MainWindow : public QMainWindow in other file

So I have in mainwindow.h :所以我在mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void SetBoxTest(const QString &Text);

[...]

and in mainwindow.cpp :mainwindow.cpp

void MainWindow::SetBoxTest(const QString &Text) {
    ui->plainTextEdit->setPlainText(Text);
}
  1. I want access SetBoxTest in other .cpp file.我想在其他.cpp文件中访问SetBoxTest I included mainwindow.h and now what?我包含了mainwindow.h ,现在呢? How to properly access SetBoxTest function?如何正确访问SetBoxTest函数?

  2. Is accessing UI in that way is correct?以这种方式访问​​ UI 是否正确?

  3. Also I saw this const QString &Text somewhere, why shouldn't I just put QString Text for such function type (which sets text in text box)?另外我在某处看到了这个const QString &Text ,为什么我不应该为这种函数类型(在文本框中设置文本)放置QString Text What's better?什么更好?

EDIT: When I try to do it like :编辑:当我尝试这样做时:

MainWindow.SetBoxTest(DataString);

or或者

MainWindow.SetBoxTest(DataString);

It says I'm missing ; before .它说我missing ; before . missing ; before .

  1. What are you trying to do exactly ?你到底想做什么? If you want to modify your MainWindow PlainTextEdit from an other UI file, you can emit a signal.如果要从其他 UI 文件修改 MainWindow PlainTextEdit,可以发出信号。

  2. Yes.是的。

  3. This is a so called « lvalue-reference to const ».这就是所谓的“对 const 的左值引用”。 It denotes a reference to a const object (here a QString).它表示对 const 对象(这里是 QString)的引用。 The fact is that if you just write :事实是,如果你只是写:

     void SetBoxTest(QString Text);

Since your QString is passed by value, it will be copied .由于您的 QString 是按值传递的,因此它将被复制 With a reference, it won't be copied at all (a reference is just an alias).使用引用,它根本不会被复制(引用只是一个别名)。 A reference is then more efficient than passing by value.引用比按值传递更有效。

However, Qt tries to optimize copies by using what they call Implicit Sharing然而,Qt 试图通过使用他们所谓的隐式共享来优化副本

  1. if your MainWindow object name is window, just do window.SetBoxTest();如果您的MainWindow对象名称是 window,只需执行window.SetBoxTest(); or uses -> if you are using a pointer或使用->如果您使用的是指针
  2. ui->plainTextEdit ... I dont see ui defined... did you used qt creator to create a form? ui->plainTextEdit ... 我没有看到 ui 定义...你用 qt creator 创建了一个表单吗?
  3. const QString &Text is passing by reference . const QString &Text通过引用传递的

Learn the C++ fundamentals, most of these points have nothing to do specifically with qt学习C++基础,这些点大部分与qt无关

I want access SetBoxTest in other .cpp file.我想在其他 .cpp 文件中访问 SetBoxTest。 I included "mainwindow.h" and now what?我包含了“mainwindow.h”,现在呢? How to properly access SetBoxTest function?如何正确访问 SetBoxTest 函数?

In addition to including "mainwindow.h", you just need a pointer to your main window, and then you can call window->SetBoxTest("Hello World");除了包含“mainwindow.h”,你只需要一个指向你的主窗口的指针,然后你就可以调用window->SetBoxTest("Hello World");

Is accessing UI in that way is correct?以这种方式访问​​ UI 是否正确?

This is a pretty loaded question.这是一个非常重要的问题。 My opinion is yes that is good, much better than letting other classes access your main window's UI directly.我的意见是是的,这很好,比让其他类直接访问主窗口的 UI 好得多。

Also I saw this "const QString &Text" somewhere, why shouldn't I just put "QString Text" for such function type (which sets text in text box)?另外我在某处看到了这个“const QString &Text”,为什么我不应该为这种函数类型(在文本框中设置文本)放置“QString Text”? What's better?什么更好?

Generally, const QString &text is better because you are passing a reference, and that takes less time than QString text , which is passing a copy.通常, const QString &text更好,因为您正在传递引用,并且比传递副本的QString text花费的时间更少。 See here for an explanation.请参阅此处以获取解释。

  1. is in the other cpp file a instance of mainwindow running?在另一个 cpp 文件中是否有一个正在运行的 mainwindow 实例?
  2. maybe ... we will see what your first answer is ;)也许......我们会看到你的第一个答案是什么;)
  3. the const QString& is just more secure. const QString&更安全。 if you remove the const from your code, it wont change anything but it ensures you, that you cant change it/get a compile error if you try.如果您从代码中删除 const,它不会更改任何内容,但可以确保您无法更改它/如果您尝试,则会出现编译错误。 the reference is so you have a qstring and give it to the function ... not just create it cuz you need it.引用是这样你有一个 qstring 并将它提供给函数......不仅仅是创建它因为你需要它。

//edit: and the reference will maybe save you some memory cause it wont copy the whole string just the pointer. //edit: 引用可能会为您节省一些内存,因为它不会只复制指针的整个字符串。

soo long zai秀龙仔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM