1

So I have in 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:

void MainWindow::SetBoxTest(const QString &Text) {
    ui->plainTextEdit->setPlainText(Text);
}
  1. I want access SetBoxTest in other .cpp file. I included mainwindow.h and now what? How to properly access SetBoxTest function?

  2. Is accessing UI in that way is correct?

  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)? What's better?

EDIT: When I try to do it like :

MainWindow.SetBoxTest(DataString);

or

MainWindow.SetBoxTest(DataString);

It says I'm missing ; before .

NG_
  • 6,895
  • 7
  • 45
  • 67
dj8000
  • 209
  • 6
  • 13

4 Answers4

4
  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.

  2. Yes.

  3. This is a so called « lvalue-reference to const ». It denotes a reference to a const object (here a QString). The fact is that if you just write :

    void SetBoxTest(QString Text);
    

Since your QString is passed by value, it will be copied. 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

octal
  • 363
  • 3
  • 8
1
  1. if your MainWindow object name is window, just do 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?
  3. const QString &Text is passing by reference.

Learn the C++ fundamentals, most of these points have nothing to do specifically with qt

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
1

I want access SetBoxTest in other .cpp file. I included "mainwindow.h" and now what? How to properly access SetBoxTest function?

In addition to including "mainwindow.h", you just need a pointer to your main window, and then you can call window->SetBoxTest("Hello World");

Is accessing UI in that way is correct?

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.

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)? 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. See here for an explanation.

Community
  • 1
  • 1
Anthony
  • 8,570
  • 3
  • 38
  • 46
  • Say, for example, in your main window, you create a dialog with a parent of `this` (the main window). Then in your dialog, you can call `static_cast(parent())->SetBoxText("Some text");` I would look carefully at the [docs for QObject](http://doc.qt.digia.com/qt/qobject.html) for more info about that. – Anthony Nov 08 '12 at 16:06
  • I don't get it. When doing it like that it says : 'SetBoxText' : is not a member of 'MainWindow' – dj8000 Nov 08 '12 at 19:46
  • 1
    Er, it should be "SetBoxTest", not "SetBoxText". – Anthony Nov 08 '12 at 20:11
  • Also not working "left of '->SetBoxTest' must point to class/struct/union/generic type type is ''unknown-type''". But why eg. this : MainWindow w; w.SetBoxTest("test"); is not working too. This one is compiling, but when using this function with button click does nothing. – dj8000 Nov 08 '12 at 20:15
  • Looking at the edit in your question, note that "MainWindow.setBoxTest(DataString)" is not correct, because "MainWindow" is the name of your class, not the name of your object of that class. How exactly are you calling the function `setBoxTest`? – Anthony Nov 08 '12 at 20:32
  • That's the problem - I don't now how to call it that way to make it work. I'm trying to call it in other .cpp file, in function, that I'll use in main file (eg. on button click). – dj8000 Nov 08 '12 at 20:35
  • In order to be able to call methods on your main window (an object of class MainWindow), you need to be able to refer to it, either directly (i.e., in your main function), or via a pointer. Generally the main window should give other objects a pointer to itself (`this`) if you want those objects to be able to call its methods. If another object inherits from QObject, and its parent is the main window, then in any one of that object's methods, you can call `static_cast(parent())->SetBoxTest("Some text");`, provided you have included mainwindow.h at the top of that cpp file. – Anthony Nov 08 '12 at 21:58
  • I've tried do that. Errors : `'parent': identifier not found` and `left of '->SetBoxTest' must point to class/struct/union/generic type/ type is ''unknown-type''` – dj8000 Nov 09 '12 at 11:35
  • Anthony: Have a look here http://stackoverflow.com/questions/13310694/qt-accessing-mainwindow-function-from-other-file – dj8000 Nov 09 '12 at 15:19
0
  1. is in the other cpp file a instance of mainwindow running?
  2. maybe ... we will see what your first answer is ;)
  3. the const QString& is just more secure. 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. the reference is so you have a qstring and give it to the function ... not just create it cuz you need it.

//edit: and the reference will maybe save you some memory cause it wont copy the whole string just the pointer.

soo long zai

Zaiborg
  • 2,492
  • 19
  • 28