[SOLVED] textEdit not letting me access any of its functions
-
Hello,
I am trying to run what seems to be a very simple example. I have done it many times, but now that I have it incorporated into a larger project with multiple classes it does not seem to want to work. I have the following code:
void BasicGui::displayProgram(QString aPath)
{
QFile programFile(aPath);
programFile.open(QIODevice::ReadOnly);
QTextStream textStream(&programFile);
QString line = textStream.readAll();
programFile.close();qDebug() << line; ui->programTextEdit->setPlainText(line); ui->runButton->setEnabled(true); ui->pauseButton->setEnabled(true);
}
I am just trying to print out the contents of "line" to my textEdit box called "programTextEdit". I know I am getting info because I can print out line in my debug window. However, when I try the function setPlainText() or anything within the textEdit features I get the following error:
error: C2227: left of '->programTextEdit' must point to class/struct/union/generic type
I don't understand how this is possible. Any thoughts...
-
first of all use code tags.
the error message tells you that the compiler doesn't know the type of "programTextEdit". Did you include your ui-header file?
-
Hi and welcome to devnet,
Please encode your code in coding tags that will make it readable.
Your are missing
@#include <QTextEdit>@in your cpp file so the compiler doesn't know what it is
edit: getting closer :D
edit2: I missread the error, you are missing your ui_xxx include. The compiler doesn't know what ui is. -
Hey, thanks for the quick responses and sorry about the code tags.
I did include ui_basicgui.h in my project. It wasn't <QTextEdit> either. Here is the code again:
@#include "basicgui.h"
#include "ui_basicgui.h"
#include "loadfile.h"
#include "ui_loadfile.h"
#include "ui_selectfile.h"
#include "selectfile.h"
#include <QtDebug>BasicGui::BasicGui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::BasicGui)
{
ui->setupUi(this);
}BasicGui::~BasicGui()
{
delete ui;
}void BasicGui::on_loadProgramButton_clicked()
{
LoadFile loadFileDialog;
loadFileDialog.setModal(true);
loadFileDialog.exec();
}void BasicGui::on_selectProgramButton_clicked()
{
SelectFile selectFileDialog;
selectFileDialog.setModal(true);
selectFileDialog.exec();
}void BasicGui::displayProgram(QString aPath)
{
QFile programFile(aPath);
programFile.open(QIODevice::ReadOnly);
QTextStream textStream(&programFile);
QString line = textStream.readAll();
programFile.close();qDebug() << line; ui->programTextEdit->setPlainText(line); ui->runButton->setEnabled(true); ui->pauseButton->setEnabled(true);
}
@ -
and you are sure Ui::BasicGui has a QTextEdit named "programTextEdit"?
If so, try to clean your project, run qmake again and build the project again. -
I checked the ui_basicgui.h header and found it there:
@class Ui_BasicGui
{
public:
QWidget *centralWidget;
QGroupBox *groupBox;
QGridLayout *gridLayout;
QHBoxLayout *horizontalLayout;
QRadioButton *radioButton;
QRadioButton *radioButton_2;
QRadioButton *radioButton_3;
QPushButton *machineOnButton;
QPushButton *machineOffButton;
QGroupBox *groupBox_4;
QGridLayout *gridLayout_5;
QGroupBox *groupBox_2;
QFormLayout *formLayout;
QLineEdit *lineEdit;
QGridLayout *gridLayout_2;
QPushButton *zPlusButton;
QPushButton *yPlusButton;
QPushButton *xMinusButton;
QPushButton *xPlusButton;
QPushButton *yMinusButton;
QPushButton *zMinusButton;
QGroupBox *groupBox_8;
QGridLayout *gridLayout_3;
QRadioButton *radioButton_4;
QRadioButton *radioButton_5;
QGroupBox *groupBox_6;
QGridLayout *gridLayout_7;
QTextEdit *programTextEdit;
QPushButton *selectProgramButton;
QGroupBox *groupBox_7;
QGridLayout *gridLayout_8;
QSlider *horizontalSlider;
QPushButton *eStopButton;
QWidget *layoutWidget;
QVBoxLayout *verticalLayout_4;
QGroupBox *groupBox_3;
QGridLayout *gridLayout_10;
QGridLayout *gridLayout_4;
QLabel *label_4;
QLabel *xPosActualLabel;
QLabel *label_5;
QLabel *yPosActualLabel;
QLabel *label_6;
QLabel *zPosActualLabel;
QGroupBox *groupBox_5;
QGridLayout *gridLayout_11;
QGridLayout *gridLayout_6;
QLabel *label_7;
QLabel *xVelActualLabel;
QLabel *label_8;
QLabel *yVelActualLabel;
QLabel *label_9;
QLabel *zVelActualLabel;
QWidget *layoutWidget1;
QHBoxLayout *horizontalLayout_3;
QPushButton *runButton;
QPushButton *pauseButton;
QPushButton *loadProgramButton;
QToolBar *mainToolBar;
QStatusBar *statusBar;@ -
[quote author="raven-worx" date="1368705257"]If so, try to clean your project, run qmake again and build the project again.[/quote]
Did you do this already? If not, try it please.
-
Yes, I did clean the project and run qmake. Sorry, I forgot to mention that in my last post.
-
Just to clarify, nothing in my textEdit ("programTextEdit") works. I get the same error when I try accessing anything in the ui.
-
I played around. If I put the following lines:
@ ui->programTextEdit->setPlainText(line);
ui->runButton->setEnabled(true);
ui->pauseButton->setEnabled(true);@in the constructor, I get no build error. The errors only come up if I throw it in a separate function within my source file. Why is that?
-
Is your separate function a member of BasicGui ?
-
Yup. See below (displayProgram):
@#ifndef BASICGUI_H
#define BASICGUI_H#include <QMainWindow>
#include <QtGui>
#include <QtCore>namespace Ui {
class BasicGui;
}class BasicGui : public QMainWindow
{
Q_OBJECTpublic:
explicit BasicGui(QWidget *parent = 0);
~BasicGui();
static void displayProgram(QString);private slots:
void on_selectProgramButton_clicked();
void on_loadProgramButton_clicked();private:
Ui::BasicGui *ui;};
#endif // BASICGUI_H@
-
So you are trying to modify a private class member from a static function ?
That's not allowed
-
[quote author="SGaist" date="1368717950"]So you are trying to modify a private class member from a static function ?
That's not allowed[/quote]
OK, I made the function non-static, but am now getting a function saying "error: C2352: 'BasicGui::displayProgram' : illegal call of non-static member function" when trying to call the function from another class/file with the following code:
@ BasicGui::displayProgram(aPath);@
-
@
ui->displayProgram(aPath);
@ -
[quote author="raven-worx" date="1368719638"]@
ui->displayProgram(aPath);
@[/quote]displayProgram() is from another class (Ui::BasicGui) so when I do that it says that displayProgram is not a member of the class that I am making the call in (which in this case is called Ui::SelectFile).
-
I got it to work. I didn't make an object of BasicGui in my SelectFile class. I did the following for it to work:
@public:
explicit SelectFile(QWidget *parent = 0);
~SelectFile();
BasicGui *basicgui;
@Then I made a call to basicgui->displayProgram()
-
Your code looks like it's getting messy. You are starting to have pointers to objects not directly related to those you are using.
May I suggest reading some of the examples from the Qt documentation ?
Typically updating your line edit content from another widget should be done using signals and slots (Unless I misunderstood your code). Or if SelectFile is a dialog to select a file (like the name suggests) why not use QFileDialog from BasicGui and directly update the line edit ?
-
OK. I will do some HW. Thanks!