Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting errors in QtCreator ui.h file
Qt 6.12 LTS is out! See what's new in the release blog

Getting errors in QtCreator ui.h file

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 957 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.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    Hi,

    I needed to have the ability to copy/paste from a QTableWidget to a text file. So what I did was created a CustomTableWidget which inherits from QTableWidget. After I created my custom class in Qt creator, I promoted the custom class to the QTableWidgets I created in QtCreator. When I try to compile I get errors in the source that calls the custom class ui_MyForm.h. This file is generated by Qt.

    I keep getting emConfigTableWidget is undeclared in this .h file.

    I use my custom class like this:
    In .h file of my main source file.
    CustomTableWidget *emConfigTableWidget;

    In the CPP file I use it as such:
    #include "ui_MyForm.h"
    #include "CustomTableWidget.h"

    MyForm(QWidget *parent) :
    QWidget(parent),
    emConfigTableWidget(new CustomTableWidget)

    emConfigTableWidget->horizontalHeader()->setSectionResizeMode(EM_CONFIG_ID_INDEX, QHeaderView::ResizeToContents);

    But I get errors in ui_MyForm.h
    emConfigTableWidget:undeclared identifier

    Would anyone know why this is happening?

    My Custom class looks like this:
    CustomTableWidget.h
    #ifndef CUSTOMTABLEWIDGET_H
    #define CUSTOMTABLEWIDGET_H

    HIDE_QT_WARNINGS_START

    #include <QTableWidget>
    #include <QObject>

        class CustomTableWidget : public QTableWidget
        {
             Q_OBJECT
    
        public:
            CustomTableWidget(QWidget *parent = 0);
            ~CustomTableWidget();
            void copy();
            void paste();
    
        private:
            QTableWidgetSelectionRange selectedRange();
    
        };
    

    #endif // CUSTOMTABLEWIDGET_H

    CustomTableWidget.cpp looks like this:
    #include "CustomTableWidget.h"
    #include <QString>
    #include <QDebug>
    #include <QApplication>
    #include <QClipboard>
    #include <QTableWidgetSelectionRange>

    CustomTableWidget(QWidget *parent) :
    QTableWidget(parent)
    {

    }

    ~CustomTableWidget()
    {

    }

    void
    CustomTableWidget::copy()
    {
    QTableWidgetSelectionRange range = selectedRange();
    QString str;
    for (int i = 0; i < range.rowCount(); ++i) {
    if (i > 0)
    str += "\n";
    for (int j = 0; j < range.columnCount(); ++j) {
    if (j > 0)
    str += "\t";
    str += itemAt( range.topRow() + i, range.leftColumn() + j)->text();
    }
    }
    str += "\n";
    QApplication::clipboard()->setText(str);

    }

    QTableWidgetSelectionRange
    CustomTableWidget::selectedRange()
    {
    QList<QTableWidgetSelectionRange> ranges = selectedRanges();
    if(ranges.isEmpty())
    {
    return QTableWidgetSelectionRange();
    }

    return ranges.first();
    }

    void
    CustomTableWidget::paste()
    {
    QString str = QApplication::clipboard()->text();
    qDebug() << str;
    QStringList rows = str.split('\n');
    int numRows = rows.count()-1;
    int numColumns = rows.first().count('\t') + 1;

    qDebug() << numRows << " " << numColumns ;
    for (int i = 0; i < numRows; ++i)
    {
        QStringList columns = rows[i].split('\t');
        for (int j = 0; j < numColumns; ++j)
        {
              QApplication::clipboard()->setText(rows[i] + columns[j], QClipboard::Clipboard);
        }
    }
    

    }

    JonBJ 1 Reply Last reply
    0
    • L leinad

      Hi,

      I needed to have the ability to copy/paste from a QTableWidget to a text file. So what I did was created a CustomTableWidget which inherits from QTableWidget. After I created my custom class in Qt creator, I promoted the custom class to the QTableWidgets I created in QtCreator. When I try to compile I get errors in the source that calls the custom class ui_MyForm.h. This file is generated by Qt.

      I keep getting emConfigTableWidget is undeclared in this .h file.

      I use my custom class like this:
      In .h file of my main source file.
      CustomTableWidget *emConfigTableWidget;

      In the CPP file I use it as such:
      #include "ui_MyForm.h"
      #include "CustomTableWidget.h"

      MyForm(QWidget *parent) :
      QWidget(parent),
      emConfigTableWidget(new CustomTableWidget)

      emConfigTableWidget->horizontalHeader()->setSectionResizeMode(EM_CONFIG_ID_INDEX, QHeaderView::ResizeToContents);

      But I get errors in ui_MyForm.h
      emConfigTableWidget:undeclared identifier

      Would anyone know why this is happening?

      My Custom class looks like this:
      CustomTableWidget.h
      #ifndef CUSTOMTABLEWIDGET_H
      #define CUSTOMTABLEWIDGET_H

      HIDE_QT_WARNINGS_START

      #include <QTableWidget>
      #include <QObject>

          class CustomTableWidget : public QTableWidget
          {
               Q_OBJECT
      
          public:
              CustomTableWidget(QWidget *parent = 0);
              ~CustomTableWidget();
              void copy();
              void paste();
      
          private:
              QTableWidgetSelectionRange selectedRange();
      
          };
      

      #endif // CUSTOMTABLEWIDGET_H

      CustomTableWidget.cpp looks like this:
      #include "CustomTableWidget.h"
      #include <QString>
      #include <QDebug>
      #include <QApplication>
      #include <QClipboard>
      #include <QTableWidgetSelectionRange>

      CustomTableWidget(QWidget *parent) :
      QTableWidget(parent)
      {

      }

      ~CustomTableWidget()
      {

      }

      void
      CustomTableWidget::copy()
      {
      QTableWidgetSelectionRange range = selectedRange();
      QString str;
      for (int i = 0; i < range.rowCount(); ++i) {
      if (i > 0)
      str += "\n";
      for (int j = 0; j < range.columnCount(); ++j) {
      if (j > 0)
      str += "\t";
      str += itemAt( range.topRow() + i, range.leftColumn() + j)->text();
      }
      }
      str += "\n";
      QApplication::clipboard()->setText(str);

      }

      QTableWidgetSelectionRange
      CustomTableWidget::selectedRange()
      {
      QList<QTableWidgetSelectionRange> ranges = selectedRanges();
      if(ranges.isEmpty())
      {
      return QTableWidgetSelectionRange();
      }

      return ranges.first();
      }

      void
      CustomTableWidget::paste()
      {
      QString str = QApplication::clipboard()->text();
      qDebug() << str;
      QStringList rows = str.split('\n');
      int numRows = rows.count()-1;
      int numColumns = rows.first().count('\t') + 1;

      qDebug() << numRows << " " << numColumns ;
      for (int i = 0; i < numRows; ++i)
      {
          QStringList columns = rows[i].split('\t');
          for (int j = 0; j < numColumns; ++j)
          {
                QApplication::clipboard()->setText(rows[i] + columns[j], QClipboard::Clipboard);
          }
      }
      

      }

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @leinad said in Getting errors in QtCreator ui.h file:

      But I get errors in ui_MyForm.h
      emConfigTableWidget:undeclared identifier

      If you get an error in a .h file show that line, and the lines around it.

      #include "ui_MyForm.h"
      #include "CustomTableWidget.h"
      

      But I get errors in ui_MyForm.h

      emConfigTableWidget:undeclared identifier

      If CustomTableWidget is declared in CustomTableWidget.h, and that is included after ui_MyForm.h, and that has CustomTableWidget *emConfigTableWidget; in it, then what is CustomTableWidget at the time that statement is encountered? Should those include lines be reversed?

      1 Reply Last reply
      1
      • L Offline
        L Offline
        leinad
        wrote on last edited by
        #3

        I reversed them as you said but I still get the same results. I did a clean and also reran run qmake. Now I get "ui_myform.h" file not found.

        JonBJ 1 Reply Last reply
        0
        • L Offline
          L Offline
          leinad
          wrote on last edited by
          #4

          but indeed ui_myForm.h is in the debug folder so not sure why that error is occurring.

          1 Reply Last reply
          0
          • L leinad

            I reversed them as you said but I still get the same results. I did a clean and also reran run qmake. Now I get "ui_myform.h" file not found.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @leinad said in Getting errors in QtCreator ui.h file:

            I did a clean and also reran run qmake. Now I get "ui_myform.h" file not found.

            Running the first make/build after this should run uic to regenerate this file in the debug or release directory, depending on which configuration you are building for. Is it indeed doing so? You should also be able to delete all the files in the debug/release directory (not the source one!) to restart from a really clean state.

            Get back to original state. Find exactly which line in which file is causing emConfigTableWidget is undeclared message. Figure out how you think emConfigTableWidget should have been declared to the compiler from that file or files which were included before it.

            1 Reply Last reply
            1

            • Login

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