Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How To Solve This 'ui' was not declared in this scope

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

Scheduled Pinned Locked Moved Unsolved C++ Gurus
5 Posts 3 Posters 8.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kistlak
    wrote on 7 Dec 2017, 10:59 last edited by
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 7 Dec 2017, 11:30 last edited by hskoglund 12 Jul 2017, 11:40
      #2

      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);
      ...
      
      K J 2 Replies Last reply 7 Dec 2017, 11:58
      0
      • H hskoglund
        7 Dec 2017, 11:30

        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);
        ...
        
        K Offline
        K Offline
        Kistlak
        wrote on 7 Dec 2017, 11:58 last edited by
        #3

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

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hskoglund
          wrote on 7 Dec 2017, 12:16 last edited by
          #4

          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)

          1 Reply Last reply
          2
          • H hskoglund
            7 Dec 2017, 11:30

            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);
            ...
            
            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 7 Dec 2017, 13:44 last edited by
            #5

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2

            1/5

            7 Dec 2017, 10:59

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved