Getting errors in QtCreator ui.h file
-
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 identifierWould anyone know why this is happening?
My Custom class looks like this:
CustomTableWidget.h
#ifndef CUSTOMTABLEWIDGET_H
#define CUSTOMTABLEWIDGET_HHIDE_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); } }}
-
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 identifierWould anyone know why this is happening?
My Custom class looks like this:
CustomTableWidget.h
#ifndef CUSTOMTABLEWIDGET_H
#define CUSTOMTABLEWIDGET_HHIDE_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); } }}
@leinad said in Getting errors in QtCreator ui.h file:
But I get errors in ui_MyForm.h
emConfigTableWidget:undeclared identifierIf you get an error in a
.hfile 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
CustomTableWidgetis declared inCustomTableWidget.h, and that is included afterui_MyForm.h, and that hasCustomTableWidget *emConfigTableWidget;in it, then what isCustomTableWidgetat the time that statement is encountered? Should those include lines be reversed? -
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.
@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
uicto 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 undeclaredmessage. Figure out how you thinkemConfigTableWidgetshould have been declared to the compiler from that file or files which were included before it.