Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Accessing widgets from my class
QtWS25 Last Chance

Accessing widgets from my class

Scheduled Pinned Locked Moved General and Desktop
29 Posts 3 Posters 11.5k 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.
  • M Offline
    M Offline
    MuldeR
    wrote on last edited by
    #14

    [quote author="Flurite" date="1335222560"]
    Anyhow, the errors were as mentioned in a previous post this:

    invalid use of incomplete struct type ‘struct Ui::Clock_Application’ --- at line 40
    forward declaration of ‘struct Ui::Clock_Application’ --- at line 11
    invalid use of incomplete struct type ‘struct Ui::Clock_Application’ --- at line 73
    forward declaration of ‘struct Ui::Clock_Application’ --- at line 11,

    Lines 40 and 73 are lines in my changeValue() method that try and access the widget.. not surprising lol..[/quote]

    There are different methods to use the .ui files generated with Qt Designer:
    http://doc-snapshot.qt-project.org/4.8/designer-using-a-ui-file.html

    It seems that you have decided for the "Using a Pointer Member Variable" method:
    http://doc-snapshot.qt-project.org/4.8/designer-using-a-ui-file.html#using-a-pointer-member-variable

    However it seems like you are missing to include the header file (.h) generated by the UIC.
    You have the forward declaration (probably in your header file), but are missing the actual definition!
    Make sure you include the header file generated by UIC in your source file...

    My OpenSource software at: http://muldersoft.com/

    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

    Go visit the coop: http://youtu.be/Jay...

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Flurite
      wrote on last edited by
      #15

      [quote author="MuldeR" date="1335223272"]
      [quote author="Flurite" date="1335222560"]
      Anyhow, the errors were as mentioned in a previous post this:

      invalid use of incomplete struct type ‘struct Ui::Clock_Application’ --- at line 40
      forward declaration of ‘struct Ui::Clock_Application’ --- at line 11
      invalid use of incomplete struct type ‘struct Ui::Clock_Application’ --- at line 73
      forward declaration of ‘struct Ui::Clock_Application’ --- at line 11,

      Lines 40 and 73 are lines in my changeValue() method that try and access the widget.. not surprising lol..[/quote]

      There are different methods to use the .ui files generated with Qt Designer:
      http://doc-snapshot.qt-project.org/4.8/designer-using-a-ui-file.html

      It seems that you have decided for the "Using a Pointer Member Variable" method:
      http://doc-snapshot.qt-project.org/4.8/designer-using-a-ui-file.html#using-a-pointer-member-variable

      However it seems like you are missing to include the header file (.h) generated by the UIC.
      You have the forward declaration (probably in your header file), but are missing the actual definition!
      Make sure you include the header file generated by UIC in your source file...[/quote]

      No wait, you're saying that I did not include the header file in my clock_application.cpp file? I think I did include that. These are the files I have included in my clock_application.cpp file:

      #include "clock_application.h"
      #include "ui_clock_application.h"
      #include "QString"
      #include "QRegExp"
      #include "vector"
      #include "QThread"

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #16

        “ui_clock_application.h” is what the UIC created from your .UI file, right?

        Did you inspect that file? In you case it should contain:

        @/********************************************************************************
        ** Form generated from reading UI file 'Clock_Application.ui'
        **
        ** Created: Sun 22. Apr 02:04:07 2012
        ** by: Qt User Interface Compiler version 4.8.1
        **
        ** WARNING! All changes made in this file will be lost when recompiling UI file!
        ********************************************************************************/

        ...

        QT_BEGIN_NAMESPACE

        class Ui_Clock_Application
        {
        public:

        ....
        

        }

        namespace Ui {
        class Clock_Application: public Ui_Clock_Application {};
        }@

        If that header file does not define Ui::Clock_Application, then that explains why you get the error.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Flurite
          wrote on last edited by
          #17

          I do not know. It's included in the .cpp file, but I cannot find it in the project's folder.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #18

            Actually your compiler would throw an error, if you include a file that does not exist.

            So it has to exist - somewhere.

            If you create GUI's with the Qt Designer, you first get a .UI file. But C++ doesn't know anything about .UI files.

            The UIC program needs to be used to create a C++ header file (.h) from your UI file.

            Then the header file created by UIC can be included. It contains the required definitions, of Ui::MyDialgClass.

            In your case, it should contain the definition of Ui::Clock_Application. Otherwise you have a problem...

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Flurite
              wrote on last edited by
              #19

              Hmm.. I'm positive that I used Qt Designer.. but I'm somehow doubting that's the problem because my program will run if I reset it to what it originally was.

              clock_application.h contains my definition of Clock_Application

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #20

                Defining your own class is not sufficient.

                Somehow the code generated by UIC (based on your .UI file from the Qt Designer) needs to be incorporated.

                And Qt offers different ways to do that. You apparently chose the "Using a Pointer Member Variable" way.

                Please carefully read:
                http://doc-snapshot.qt-project.org/4.8/designer-using-a-ui-file.html

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Flurite
                  wrote on last edited by
                  #21

                  Sorry I'm probably being really dumb right now.

                  Anyway, I read it, but I am not understanding how this will help me. The header file that contains the code you said previously is called clock_application.h

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on last edited by
                    #22

                    Well, did you manage to locate the header file generated by UIC?

                    If so, did you inspect that file to make sure it contains a definition of Ui::Clock_Application ???

                    In your code you use the Ui::Clock_Application class (note hat that is one is different from the Clock_Application class without the "Ui::"). This requires that this class is defined somewhere. Your error message indicates that you have a forward-declaration of that class, but no definition of it! A forward-declaration is sufficient to declare the pointer member variable in your own class, but as soon as you want to dereference the pointer, a full definition is required. That definition usually is given in the header file generated by UIC (from your .UI file).

                    My OpenSource software at: http://muldersoft.com/

                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                    Go visit the coop: http://youtu.be/Jay...

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Flurite
                      wrote on last edited by
                      #23

                      Aha, I found it! I just recalled there are two folders for a project! Let me copy and paste the code for you. It's a long piece.

                      @
                      /********************************************************************************
                      ** Form generated from reading UI file 'clock_application.ui'
                      **
                      ** Created: Sat Mar 17 18:50:58 2012
                      ** by: Qt User Interface Compiler version 4.7.4
                      **
                      ** WARNING! All changes made in this file will be lost when recompiling UI file!
                      ********************************************************************************/

                      #ifndef UI_CLOCK_APPLICATION_H
                      #define UI_CLOCK_APPLICATION_H

                      #include <QtCore/QVariant>
                      #include <QtGui/QAction>
                      #include <QtGui/QApplication>
                      #include <QtGui/QButtonGroup>
                      #include <QtGui/QHeaderView>
                      #include <QtGui/QLineEdit>
                      #include <QtGui/QMainWindow>
                      #include <QtGui/QPushButton>
                      #include <QtGui/QStatusBar>
                      #include <QtGui/QTabWidget>
                      #include <QtGui/QToolBar>
                      #include <QtGui/QWidget>

                      QT_BEGIN_NAMESPACE

                      class Ui_Clock_Application
                      {
                      public:
                      QWidget *centralWidget;
                      QTabWidget *ClockSet;
                      QWidget *Stopwatch;
                      QLineEdit *Output;
                      QPushButton *Start;
                      QPushButton *Pause;
                      QPushButton *Reset;
                      QWidget *Alarm;
                      QToolBar *mainToolBar;
                      QStatusBar *statusBar;

                      void setupUi(QMainWindow *Clock_Application)
                      {
                          if (Clock_Application->objectName().isEmpty())
                              Clock_Application->setObjectName(QString::fromUtf8("Clock_Application"));
                          Clock_Application->resize(400, 300);
                          centralWidget = new QWidget(Clock_Application);
                          centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
                          ClockSet = new QTabWidget(centralWidget);
                          ClockSet->setObjectName(QString::fromUtf8("ClockSet"));
                          ClockSet->setGeometry(QRect(10, 0, 371, 241));
                          ClockSet->setTabPosition(QTabWidget::North);
                          ClockSet->setTabShape(QTabWidget::Rounded);
                          Stopwatch = new QWidget();
                          Stopwatch->setObjectName(QString::fromUtf8("Stopwatch"));
                          Output = new QLineEdit(Stopwatch);
                          Output->setObjectName(QString::fromUtf8("Output"));
                          Output->setGeometry(QRect(120, 50, 131, 31));
                          Output->setAlignment(Qt::AlignCenter);
                          Start = new QPushButton(Stopwatch);
                          Start->setObjectName(QString::fromUtf8("Start"));
                          Start->setGeometry(QRect(40, 110, 81, 31));
                          Pause = new QPushButton(Stopwatch);
                          Pause->setObjectName(QString::fromUtf8("Pause"));
                          Pause->setGeometry(QRect(150, 110, 81, 31));
                          Reset = new QPushButton(Stopwatch);
                          Reset->setObjectName(QString::fromUtf8("Reset"));
                          Reset->setGeometry(QRect(250, 110, 81, 31));
                          ClockSet->addTab(Stopwatch, QString());
                          Alarm = new QWidget();
                          Alarm->setObjectName(QString::fromUtf8("Alarm"));
                          ClockSet->addTab(Alarm, QString());
                          Clock_Application->setCentralWidget(centralWidget);
                          mainToolBar = new QToolBar(Clock_Application);
                          mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
                          Clock_Application->addToolBar(Qt::TopToolBarArea, mainToolBar);
                          statusBar = new QStatusBar(Clock_Application);
                          statusBar->setObjectName(QString::fromUtf8("statusBar"));
                          Clock_Application->setStatusBar(statusBar);
                      
                          retranslateUi(Clock_Application);
                          QObject::connect(Start, SIGNAL(clicked()), Clock_Application, SLOT(timer_Start()));
                      
                          ClockSet->setCurrentIndex(0);
                      
                      
                          QMetaObject::connectSlotsByName(Clock_Application);
                      } // setupUi
                      
                      void retranslateUi(QMainWindow *Clock_Application)
                      {
                          Clock_Application->setWindowTitle(QApplication::translate("Clock_Application", "Clock_Application", 0, QApplication::UnicodeUTF8));
                          Output->setText(QApplication::translate("Clock_Application", "00:00:00", 0, QApplication::UnicodeUTF8));
                          Start->setText(QApplication::translate("Clock_Application", "Start", 0, QApplication::UnicodeUTF8));
                          Pause->setText(QApplication::translate("Clock_Application", "Pause", 0, QApplication::UnicodeUTF8));
                          Reset->setText(QApplication::translate("Clock_Application", "Reset", 0, QApplication::UnicodeUTF8));
                          ClockSet->setTabText(ClockSet->indexOf(Stopwatch), QApplication::translate("Clock_Application", "Stopwatch", 0, QApplication::UnicodeUTF8));
                          ClockSet->setTabText(ClockSet->indexOf(Alarm), QApplication::translate("Clock_Application", "Alarm", 0, QApplication::UnicodeUTF8));
                      } // retranslateUi
                      

                      };

                      namespace Ui {
                      class Clock_Application: public Ui_Clock_Application {};
                      } // namespace Ui

                      QT_END_NAMESPACE

                      #endif // UI_CLOCK_APPLICATION_H
                      @

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        MuldeR
                        wrote on last edited by
                        #24

                        So Ui::Clock_Application is defined (lines 103 to 105).

                        But then I don't understand why the compiler throws the following error:
                        @invalid use of incomplete struct type ‘struct Ui::Clock_Application’ @

                        Are you 100% sure the source code file that throws this error includes the exactly same header file you quoted?

                        My OpenSource software at: http://muldersoft.com/

                        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                        Go visit the coop: http://youtu.be/Jay...

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          Flurite
                          wrote on last edited by
                          #25

                          I've had a problem like that before with mutual friendship classes. I fixed it by simply defining the functions after both classes were defined. Obviously, this is not the problem in this case..

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            MuldeR
                            wrote on last edited by
                            #26

                            Time to post your complete clock_application.h and clock_application.cpp, I think.

                            My OpenSource software at: http://muldersoft.com/

                            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                            Go visit the coop: http://youtu.be/Jay...

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              Flurite
                              wrote on last edited by
                              #27

                              clock_application.h:

                              @

                              #ifndef CLOCK_APPLICATION_H
                              #define CLOCK_APPLICATION_H

                              #include <QMainWindow>
                              #include <QTimer>
                              #include <QLineEdit>

                              namespace Ui {
                              class Clock_Application;
                              }

                              class Clock_Application : public QMainWindow
                              {
                              Q_OBJECT

                              public:
                              explicit Clock_Application(QWidget *parent = 0);
                              ~Clock_Application();
                              public slots:
                              void timer_Start();
                              void changeTime();
                              private:
                              Ui::Clock_Application ui;
                              QTimer
                              timer;
                              };

                              void Clock_Application::timer_Start()
                              {
                              timer = new QTimer();
                              connect(timer, SIGNAL(timeout()), this, SLOT(changeTime()));
                              timer->start(1000);
                              }

                              void Clock_Application::changeTime()
                              {
                              std::vector<int> times;
                              QRegExp findTimeSequences("(\d+)");
                              QString currentTime(ui->Output->text());

                              int posOffset(0);
                              while ((posOffset = findTimeSequences.indexIn(currentTime, posOffset)) != -1)
                              {
                                  times.push_back(findTimeSequences.cap(1).toInt());
                                  posOffset += findTimeSequences.matchedLength();
                              }
                              
                              times[2] += 1;
                              
                              QString outputTime;
                              for (int i = (int) times.size() - 1; i >= 0; i--)
                              {
                                  if (times[i] == 60 && i > 0)
                                  {
                                      times[i-1] += 1;
                                      times[i] = 0;
                                  }
                              
                                  QString currentNum; currentNum.setNum(times[i]);
                                  if (times[i] <= 9 && i >= 0)
                                  {
                                      currentNum.insert(0, QString("0"));
                                  }
                                  if (i < 2)
                                  {
                                      currentNum.append(":");
                                  }
                              
                                  outputTime.insert(0, currentNum);
                              }
                              
                              ui->Output->setText(outputTime);
                              

                              }

                              #endif // CLOCK_APPLICATION_H
                              @

                              clock_application.cpp:

                              @

                              #include "clock_application.h"
                              #include "ui_clock_application.h"
                              #include "QString"
                              #include "QRegExp"
                              #include "vector"
                              #include "QThread"

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

                              Clock_Application::~Clock_Application()
                              {
                              delete ui;
                              }
                              @

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                MuldeR
                                wrote on last edited by
                                #28

                                In your header file you are trying to dereference ui, although you only have a forward-declartion of Ui::Clock_Application there! This can't work, obviously. The definition of the Ui::Clock_Application class in "ui_clock_application.h" is included in your .cpp file only! Simply move Clock_Application::changeTime() and Clock_Application::timer_Start() into the .cpp file. These don't belong into the header file anyway...

                                Solution:

                                clock_application.h:
                                @#ifndef CLOCK_APPLICATION_H
                                #define CLOCK_APPLICATION_H

                                #include <QMainWindow>
                                #include <QTimer>
                                #include <QLineEdit>

                                namespace Ui {
                                class Clock_Application; //Only forward-declartion here !!!
                                }

                                class Clock_Application : public QMainWindow
                                {
                                Q_OBJECT

                                public:
                                explicit Clock_Application(QWidget *parent = 0);
                                ~Clock_Application();
                                public slots:
                                void timer_Start();
                                void changeTime();
                                private:
                                Ui::Clock_Application ui; //forward-declartion sufficient here
                                QTimer
                                timer;
                                };

                                #endif // CLOCK_APPLICATION_H@

                                clock_application.cpp:
                                @#include "clock_application.h"
                                #include "ui_clock_application.h" //Contains the full class definition
                                #include "QString"
                                #include "QRegExp"
                                #include "vector"
                                #include "QThread"

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

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

                                void Clock_Application::timer_Start()
                                {
                                timer = new QTimer();
                                connect(timer, SIGNAL(timeout()), this, SLOT(changeTime()));
                                timer->start(1000);
                                }

                                void Clock_Application::changeTime()
                                {
                                std::vector<int> times;
                                QRegExp findTimeSequences("(\d+)");
                                QString currentTime(ui->Output->text()); //Requires full class definition !!!

                                int posOffset(0);
                                while ((posOffset = findTimeSequences.indexIn(currentTime, posOffset)) != -1)
                                {
                                    times.push_back(findTimeSequences.cap(1).toInt());
                                    posOffset += findTimeSequences.matchedLength();
                                }
                                
                                times[2] += 1;
                                
                                QString outputTime;
                                for (int i = (int) times.size() - 1; i >= 0; i--)
                                {
                                    if (times[i] == 60 && i > 0)
                                    {
                                        times[i-1] += 1;
                                        times[i] = 0;
                                    }
                                
                                    QString currentNum; currentNum.setNum(times[i]);
                                    if (times[i] <= 9 && i >= 0)
                                    {
                                        currentNum.insert(0, QString("0"));
                                    }
                                    if (i < 2)
                                    {
                                        currentNum.append(":");
                                    }
                                
                                    outputTime.insert(0, currentNum);
                                }
                                
                                ui->Output->setText(outputTime); //Requires full class definition !!!
                                

                                }@

                                My OpenSource software at: http://muldersoft.com/

                                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                                Go visit the coop: http://youtu.be/Jay...

                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  Flurite
                                  wrote on last edited by
                                  #29

                                  Works like a charm. Thanks for all the help!

                                  1 Reply Last reply
                                  0

                                  • Login

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