How To Solve This 'ui' was not declared in this scope



  • I am creating WordPad using C++ by using QT framework. And I have created mainwindow.cpp and mainwindow.h files. Then I have created wordpad.cpp and wordpad.h files. Still, I am very new to QT and C++. So, I don't know lots of things.

    But , when I try to build the project , it gives me this error in wordpad.cpp file.

    D:\Projects & Tests\C++\Projects\WordPad\wordpad.cpp:10: error: 'ui' was not declared in this scope
         ui->textEdit->copy();
         ^
    

    I added textEdit from QT GUI.

    How can I Fix this ??

    ** Here is the mainwindow.h file.

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "wordpad.h"
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow, WordPad
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_actionCopy_triggered();
    
    protected:
        Ui::MainWindow *ui;
    
    };
    
    #endif // MAINWINDOW_H
    

    ** Here is the mainwindow.cpp file.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        this->setCentralWidget(ui->textEdit);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_actionCopy_triggered()
    {
        WordPad copymain;
    
        copymain.copy();
    }
    

    ** Here is the wordpad.h file.

    #ifndef WORDPAD_H
    #define WORDPAD_H
    
    
    class WordPad
    {
    public:
        WordPad();
        void copy();
    
    };
    
    #endif // WORDPAD_H
    

    ** Here is the wordpad.cpp file.

    #include "wordpad.h"
    
    WordPad::WordPad()
    {
    
    }
    
    void WordPad::copy()
    {
        ui->textEdit->copy();
    }
    


  • Hi, well if you look in mainwindow.h for the declaration of ui you'll see it's private to the MainWindow class, i.e. it would rather keep ui to itself and not share it with other classes. And it does not to help to add WordPad as a base class for MainWindow, pls change back to: class MainWindow : public QMainWindow

    That said, what you can do, is pass a copy/ptr to ui to you WordPad class, say you make a constructor for WordPad that accepts a ui ptr, like this:

    #ifndef WORDPAD_H
    #define WORDPAD_H
    
    amespace Ui {
    class MainWindow;
    }
    
    class WordPad
    {
    public:
        WordPad(Ui::MainWindow* ui);
        void copy();
    
        Ui::MainWindow* uiCopy;
    };
    
    #endif // WORDPAD_H
    

    Note that you have to #include "ui_mainwindow.h" and declare the Ui namespace same way that mainwindow.cpp does.

    Then you can write you wordpad.cpp like this:

    #include "wordpad.h"
    #include "ui_mainwindow.h"
    
    WordPad::WordPad(Ui::MainWindow* ui)
    {
        uiCopy = ui;
    }
    
    void WordPad::copy()
    {
        uiCopy->textEdit->copy();;
    }
    

    Edit: forgot, here's an example of you how to pass the copy from MainWindow:

    #include "wordpad.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        auto w = new WordPad(ui);
    ...
    


  • @hskoglund said in How To Solve This 'ui' was not declared in this scope:

    uiCopy

    Now it gives me this error in wordpad.h -

    D:\Projects & Tests\C++\Projects\WordPad\wordpad.h:11: error: 'Ui' does not name a type
         Ui::MainWindow* uiCopy;
         ^
    

    And this in mainwindow.h

    D:\Projects & Tests\C++\Projects\WordPad\mainwindow.h:5: In file included from ..\WordPad\mainwindow.h:5:0,
    

    How can I Fix this ??

    I am still very new to this QT and its complicated.



  • No problem, maybe a mistyping, check that you have

    #ifndef WORDPAD_H
    #define WORDPAD_H
    
    namespace Ui {
    class MainWindow;
    }
    

    at the top of wordpad.h (note: I made mistake above, typed amespace should be namespace)


  • Moderators

    @hskoglund @Kistlak I would not pass a pointer to main UI to WordPad. From design point of view it is not a good solution as WordPad should not know anything about main UI internals. It would be better to use signals/slots or getter/setter methods in MainWindow and WordPad.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.