0

I am currently working in QT and recently I noticed something that really confused me. As far as I can tell normally when we want to create a pointer we have to use the following syntax in C++:

int number = 10;
int* pNumber = &number;

(or something similar to that)

I wanted to create a pointer to a button which was created in QT design. It was for testing purposes only. (I am new to QT and c++ so I wanted to test things out)

But then I noticed something strange that I could not understand. for some reason when I created the pointer of type "OPushButton" with the name of "button" I did not have to use the "&" with the "(*ui).pushButton_5" syntax. (pushButton_5 is the name of my button in my ui)

The code works and the text "5" is added to my "lineEdit" in QT. How does this work? Am I missing something about pointers?

Here is my code:

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QPushButton* button = (*ui).pushButton_5;
    ui->lineEdit->setText((*button).text());
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.h:

#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
  • 3
    Unrelated: instead of `(*ui).pushButton_5` you can write `ui->pushButton_5` – StefanKssmr May 01 '21 at 12:49
  • 3
    `pushButton_5` is probably already a pointer making the code work for the same reason the third assignment in the following code works: `int v = 1; int* pV = &v; int* pV2 = pV; // pV2 points to the same memory as pV for now` – fabian May 01 '21 at 12:50
  • Adding to what @fabian said, the `Ui::Name` classes created by Qt Designer do have public members with pointer type for the widgets, layouts, etc. – aschepler May 01 '21 at 13:21
  • Thank you for your fast response. I just wanted to know if it is wrong to use (*ui) instead of the arrow operator (->). I ask this because for some unknow reason my brain likes the normal dereferencing way more than the arrow operator :/ (I know I am weird) . –  May 01 '21 at 16:38

1 Answers1

2

& is not the way to create pointers, it's the way to acquire a pointer to a specific thing that you have access to.

If somebody else tells you where a thing is, you don't need the help of & to find out.

void g(int* q)
{
    int* p = q; // 'q' is the location of some unknown 'int', and so is `p`.
}

You need & if you have a thing and want to know where that thing is.

void f()
{
    int x = 5;
    int* p = &x; // The location of 'x'.
    g(&x); // Pass the location of 'x' to 'g'.
}

Also, we usually write x->y rather than (*x).y.
This convention makes a lot of sense if you look at more than one level of indirection – compare x->y->z->w to (*(*(*x).y).z).w.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thank you for your fast response. However I still have one more question. How can I figure out if something is already a pointer? (is there a special sign or something ?) for example: In the above code I am declaring a variable of pointer which is called "QPushButton" and initializing it with the dereferenced of ui (*ui) and accessing "pushButton_5" inside of it. I don't understand how can it still be a pointer? (didn't I just dereferenced it ? what is happening ? :/ I am confused ) –  May 01 '21 at 16:45
  • Look at the type of `pushButton_5` and you will see that it is `QPushButton*`; a pointer to a `QPushButton`. Also, the compiler will tell you when you get it wrong. – molbdnilo May 01 '21 at 17:30
  • But I declared "QPushButton*" myself and made it a pointer. I wanted to know how we can find out if "(*ui).pushButton_5" is already a pointer. Because didn't we already dereferenced it? –  May 02 '21 at 05:00
  • `QPushButton*` is a type (which you did not declare), and `pushButton_5` is a member variable of `Ui::MainWindow`. The `button` variable has the type `QPushButton*`, and so does the `pushButton_5` member of `Ui::MainWindow`. `(*ui). pushButton_5` dereferences `ui`, then accesses that object's `pushButton_5` member. (I would recommend a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).) – molbdnilo May 02 '21 at 05:55