Compiler complains lineEdit not declared
-
I am testing passing data from a parent window to a child window. But my problem is that the lineEdit widget (childLineEdit) in the child is reported as not having been declared. I am using QTCreator 2.7.1 and QT 5.1 beta, with MinGW 4.7_32 .
childLineEdit is clearly declared in ui_editdialog.h, and ui_editdialog.h is included in editdiaog.h on line 6. All of that done automatically by QtCreator. What is missing here?
editdialog.h
@#ifndef EDITDIALOG_H
#define EDITDIALOG_H#include <QDialog>
#include <QLineEdit>
#include "ui_editdialog.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"namespace Ui {
class editDialog;
}class editDialog : public QDialog
{
Q_OBJECTpublic:
explicit editDialog(QWidget *parent = 0);
~editDialog();
QLineEdit * parentLineEdit;private:
Ui::editDialog *ui;public slots:
void on_pushButtonClose_clicked();};
#endif // EDITDIALOG_H
@editdialog.cpp
@#include "editdialog.h"
editDialog::editDialog(QWidget *parent) : QDialog(parent), ui(new Ui::editDialog)
{
ui->setupUi(this);
childLineEdit->setText(parentLineEdit->text());
}editDialog::~editDialog()
{
delete ui;
}void editDialog::on_pushButtonClose_clicked()
{
this->close();
}
@ui_editdialog.h
@#ifndef UI_EDITDIALOG_H
#define UI_EDITDIALOG_H#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>QT_BEGIN_NAMESPACE
class Ui_editDialog
{
public:
QPushButton *pushButtonAction;
QPushButton *pushButtonClose;
QLineEdit *childLineEdit;void setupUi(QDialog *editDialog) { if (editDialog->objectName().isEmpty()) editDialog->setObjectName(QStringLiteral("editDialog")); editDialog->resize(677, 145); pushButtonAction = new QPushButton(editDialog); pushButtonAction->setObjectName(QStringLiteral("pushButtonAction")); pushButtonAction->setGeometry(QRect(120, 80, 75, 25)); pushButtonClose = new QPushButton(editDialog); pushButtonClose->setObjectName(QStringLiteral("pushButtonClose")); pushButtonClose->setGeometry(QRect(540, 80, 75, 25)); childLineEdit = new QLineEdit(editDialog); childLineEdit->setObjectName(QStringLiteral("childLineEdit")); childLineEdit->setGeometry(QRect(160, 30, 381, 25)); QFont font; font.setPointSize(10); childLineEdit->setFont(font); retranslateUi(editDialog); QMetaObject::connectSlotsByName(editDialog); } // setupUi void retranslateUi(QDialog *editDialog) { editDialog->setWindowTitle(QApplication::translate("editDialog", "Dialog", 0)); pushButtonAction->setText(QApplication::translate("editDialog", "Action", 0)); pushButtonClose->setText(QApplication::translate("editDialog", "Close", 0)); } // retranslateUi
};
namespace Ui {
class editDialog: public Ui_editDialog {};
} // namespace UiQT_END_NAMESPACE
#endif // UI_EDITDIALOG_H
@ -
Yes, as supplied by QtCreator: see below
@QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = myTest
TEMPLATE = appSOURCES += main.cpp
mainwindow.cpp
editdialog.cppHEADERS += mainwindow.h
editdialog.hFORMS += mainwindow.ui
editdialog.ui
@ -
Sorry, I misunderstood your post. childLineEdit doesn't belong to editDialog but to Ui_EditDialog
-
Thanks, I should have seen that. I changed line 6 of editDialog.c to
ui->childLineEdit->setText(parentLineEdit->text()); and it now compiles. However, I now it a segmentation fault when that line of code tries to execute.I suspect it must be somewhere in the mainwindow, but I can't find the problem, so i'm posting that code below:
mainwindow.h
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "editdialog.h"namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();public slots:
void on_pushButtonAction_clicked();
void on_pushButtonExit_clicked();private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
@mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButtonAction_clicked()
{
editDialog * dialog = (editDialog*) operator new (sizeof(editDialog));
new (dialog) editDialog;
dialog->show();
}void MainWindow::on_pushButtonExit_clicked()
{
QCoreApplication::quit();
}
@ui_mainwindow.h
@
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QWidget *centralWidget;
QPushButton *pushButtonAction;
QPushButton *pushButtonExit;
QLineEdit *lineEdit;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;void setupUi(QMainWindow *MainWindow) { if (MainWindow->objectName().isEmpty()) MainWindow->setObjectName(QStringLiteral("MainWindow")); MainWindow->resize(498, 197); QFont font; font.setPointSize(10); MainWindow->setFont(font); centralWidget = new QWidget(MainWindow); centralWidget->setObjectName(QStringLiteral("centralWidget")); pushButtonAction = new QPushButton(centralWidget); pushButtonAction->setObjectName(QStringLiteral("pushButtonAction")); pushButtonAction->setGeometry(QRect(70, 110, 75, 23)); pushButtonExit = new QPushButton(centralWidget); pushButtonExit->setObjectName(QStringLiteral("pushButtonExit")); pushButtonExit->setGeometry(QRect(360, 110, 75, 23)); lineEdit = new QLineEdit(centralWidget); lineEdit->setObjectName(QStringLiteral("lineEdit")); lineEdit->setGeometry(QRect(130, 40, 271, 20)); MainWindow->setCentralWidget(centralWidget); menuBar = new QMenuBar(MainWindow); menuBar->setObjectName(QStringLiteral("menuBar")); menuBar->setGeometry(QRect(0, 0, 498, 21)); MainWindow->setMenuBar(menuBar); mainToolBar = new QToolBar(MainWindow); mainToolBar->setObjectName(QStringLiteral("mainToolBar")); MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar); statusBar = new QStatusBar(MainWindow); statusBar->setObjectName(QStringLiteral("statusBar")); MainWindow->setStatusBar(statusBar); retranslateUi(MainWindow); QMetaObject::connectSlotsByName(MainWindow); } // setupUi void retranslateUi(QMainWindow *MainWindow) { MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0)); pushButtonAction->setText(QApplication::translate("MainWindow", "Action", 0)); pushButtonExit->setText(QApplication::translate("MainWindow", "Exit", 0)); } // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace UiQT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
@ -
You never initialize parentLineEdit
-
That's a declaration not an initialization. Your pointer points somewhere in memory not to a QLineEdit object
-
Thanks for the tip. However, I seem to be going 1 step forward and two back. I'm an old FORTRAN and C command line programer, and trying to wrap my mind around GUI programming is really been taxing.
I initialized "parentLineEdit" with:
@ QLineEdit *LineEditParent = (QLineEdit *) operator new (sizeof(QLineEdit));
new (LineEditParent) QLineEdit;ui->childLineEdit->setText(LineEditParent->text());@
The project would then compile and run, but the value from the "lineEditParent" was not picked up in editDialog. After some thought, I realized that editDialog had no awareness of its parent. So I modified the code as listed below, but now I get the following compiler error:
editdialog.cpp:3: error: no matching function for call to 'QDialog::QDialog(Ui_MainWindow&)'* (line 5 in the code below)
I have searched all over to find some example or tutorial to help with what I'm doing, but those that I find are lacking sufficient explanation to be of ay help.
#ifndef EDITDIALOG_H
#define EDITDIALOG_H#include <QDialog>
#include <QLineEdit>
#include "ui_editdialog.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"namespace Ui {
class editDialog;
}class editDialog : public QDialog
{
Q_OBJECTpublic:
explicit editDialog(Ui_MainWindow *parent = 0);
~editDialog();
QLineEdit *LineEditParent;private:
Ui::editDialog *ui;
void setLineEdit();public slots:
void on_pushButtonClose_clicked();};
#endif // EDITDIALOG_H@
#include "editdialog.h"
editDialog::editDialog(Ui_MainWindow *parent) : QDialog(parent)
{
ui->setupUi(this);
setLineEdit();
}editDialog::~editDialog()
{
delete ui;
}void editDialog::on_pushButtonClose_clicked()
{
this->close();
}void editDialog::setLineEdit()
{
QLineEdit *LineEditParent = (QLineEdit *) operator new (sizeof(QLineEdit)); //Instantiate commands from class parser
new (LineEditParent) QLineEdit;ui->childLineEdit->setText(LineEditParent->text());
}
@ -
Ok, I think I see the problem now.
First thing:
@parentLine = new QLineEdit; // C++ way of allocating an object@BUT it's not what you want to use in this case. Since you are using UI files, it means that you are using designer to build your interface.
Every widget you have created with designer is accessible through:
@ui->myWidget@
myWidget being the name given to your widget while creating them.I would recommend you to go through the examples and demo from Qt's documentation to have a grasp on how things are done. That would get you on track faster