Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

base operand of '->' has non-pointer type



  • Hello, I'm using Qt Creator 3.5.1 (opensource), Based on Qt 5.5.1 (GCC 4.9.1 20140922, 64 bit), Built on Oct 13 2015 07:38:30

    I try the Notepad code from "Getting Started Programming with Qt Widgets" from Qt at link http://doc.qt.io/qt-5/gettingstartedqt.html

    In the fileopen dialogue is a part:
    QTextStream in(&file);
    ui->textEdit->setText(in.readAll());
    file.close();

    When I run this in the Qt ide (green arrow) I get the error:
    error: base operand of '->' has non-pointer type 'Ui::NoteWindow'
    ui->textEdit->setText(in.readAll());
    .... ^
    Since I'm new to Qt and especially Qt 5.5.1, I wonder what I do wrong? because I try to run an example I take from the Qt5 site itself.

    Here the code from the example on Qt site:
    http://doc.qt.io/qt-5/gettingstartedqt.html

    void Notepad::on_actionOpen_triggered()
    {
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
    tr("Text Files (.txt);;C++ Files (.cpp *.h)"));

    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly)) {
            QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
            return;
        }
        QTextStream in(&file);
        ui->textEdit->setText(in.readAll());
        file.close();
    }
    

    }

     Thanks in advance, Nils A

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It means that it is not a pointer thus your notation is not correct. How did you declare it ?



  • Hello Thanks,

    Since I try the example from the site I did not declare the textEdit or the ui. This was done in / by the design mode of Qt5.


  • Lifetime Qt Champion

    hi
    and you have the
    notepad.ui
    as part of the project?
    also
    in mainwindow.h
    Do you have
    ...
    private:
    Ui::Notepad *ui;



  • Yes I have absolutely. I follow all steps in the http://doc.qt.io/qt-5/gettingstartedqt.html link. That's why I'm a bit surprised with the error. I'm slow in reply because I can only reply every 500 seconds as a new user ... (As a new user, you can only post once every 500 second(s) until you have earned 2 reputation - please wait before posting again)

    Nils A.


  • Lifetime Qt Champion

    can u check if u type
    ui->
    if it will suggest anything?
    also, if u dblclick the UI it opens fine etc?
    Also,
    you do have
    Ui::Notepad *ui; ?



  • @mrjj Hello,

    When I type ui it will give suggestions only after I type ::
    If I choose ui::textEdit->setText(in.readAll()); I get an error,
    Noteapp/notewindow.cpp:30: error: 'ui' is not a class or namespace
    ui::textEdit->setText(in.readAll());
    ^

    But, because this is an example application fron Qt on their site, why would they make it with errors? Do they test these apps?

    Nils A.



  • Here the files: noteapp.pro, notewindow.h and notewindow.ccp

    // noteapp.pro
    #-------------------------------------------------

    Project created by QtCreator 2015-12-27T20:58:35

    why is this so big??

    #-------------------------------------------------

    QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = Noteapp
    TEMPLATE = app

    SOURCES += main.cpp
    notewindow.cpp

    HEADERS += notewindow.h

    FORMS += notewindow.ui


    // notewindow.h
    #ifndef NOTEWINDOW_H
    #define NOTEWINDOW_H

    #include "ui_notewindow.h"

    class NoteWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit NoteWindow(QWidget *parent = 0);

    private slots:
    void on_quitButton_clicked();

    void on_actionOpen_triggered();

    void on_actionSave_triggered();

    private:
    Ui::NoteWindow ui;
    };

    #endif // NOTEWINDOW_H


    // notewindow.ccp

    #include "notewindow.h"
    #include <QFileDialog>
    #include <QFile>
    #include <QMessageBox>
    #include <QTextStream>

    NoteWindow::NoteWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    ui.setupUi(this);
    }

    void NoteWindow::on_quitButton_clicked()
    {
    qApp->quit();
    }

    void NoteWindow::on_actionOpen_triggered()
    {
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
    tr("Text Files (.txt);;C++ Files (.cpp *.h)"));

      if (!fileName.isEmpty()) {
          QFile file(fileName);
          if (!file.open(QIODevice::ReadOnly)) {
              QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
              return;
          }
          QTextStream in(&file);
          ui->textEdit->setText(in.readAll());
          file.close();
    
          //error: base operand of '->' has non-pointer type 'Ui::NoteWindow'
          //         ui->textEdit->setText(in.readAll());
          //             ^
      }
    

    }

    void NoteWindow::on_actionSave_triggered()
    {
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(),
    tr("Text Files (.txt);;C++ Files (.cpp *.h)"));

    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(QIODevice::WriteOnly)) {
            // error message
        } else {
            QTextStream stream(&file);
            stream << ui->textEdit->toPlainText();
            stream.flush();
            file.close();
        }
    }
    

    }


  • Lifetime Qt Champion

    ui is not a pointer so you should use ui.textEdit->toPlainText()



  • Yes, that's it! You are great. Why would Qt make this mistake in their example code?

    thanks,
    Nils A.


  • Lifetime Qt Champion

    There's no mistake in the example, please read again the Notepad class. In the header they are declaring a pointer for ui and the initialization also shows they are allocating a new instance of Ui::Notepad in the constructor member initializer list



  • Thanks again, but the example on the site really uses the -> and not a .

    QTextStream in(&file);
    ui->textEdit->setText(in.readAll());
    file.close();

    see http://doc.qt.io/qt-5/gettingstartedqt.html


  • Lifetime Qt Champion

    Yes, and again the example is correct.

    Your code is a bit different: you have ui as Ui::Notepad ui;. In the example it's Ui::Notepad *ui;



  • I see yes, but still strange (and an error) because the (in my case) NoteWindow class is created by the Qt-widget wizard. I did not type the code.

    example code on the site;
    #include <QMainWindow>

    namespace Ui {
    class Notepad;
    }

    class Notepad : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::Notepad *ui;
    };

    ======
    example created by the Qt-Wizard:

    #ifndef NOTEWINDOW_H
    #define NOTEWINDOW_H

    #include "ui_notewindow.h"

    class NoteWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit NoteWindow(QWidget *parent = 0);

    private:
    Ui::NoteWindow ui;
    };

    #endif // NOTEWINDOW_H



  • @SGaist Thanks again Champ. You are right, there is no error in the example. The mixing of the example code (using the *) with the auto-generate code (not using the *) made my C++ virgin brain confused. But now I will never forget to check for *'s any more.
    Have a good day! (now how to set this to solved?) (Ah, found this too. Solved!)