PDA

View Full Version : Facing problem in creating new cpp and Header file



parulkalra14
29th January 2014, 10:03
I want to create cpp and header file for this function.


void MainWindow::labtext()//labeltext function
{
if(cursor.block().text().contains("",Qt::CaseInsensitive))
{
ui->label->setText("");

}


if(cursor.block().text().contains("printf",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive)||cursor.block().text().conta ins("cout",Qt::CaseInsensitive)&&cursor.block().text().contains("<<",Qt::CaseInsensitive))
{
ui->label->setText("Printing to Standard Output");
}

else if(cursor.block().text().contains("while",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive))
{
ui->label->setText("Checking condition for While Statement");
//QMessageBox::critical(this,"oo","fdgffh");
}




else if(cursor.block().text().contains("switch",Qt::CaseInsensitive)&&cursor.block().text().contains("(",Qt::CaseInsensitive))
{
ui->label->setText("Checking condition for Switch Statement");
}
else if(cursor.block().text().contains("if",Qt::CaseInsensitive)&&cursor.block().text().contains("(",Qt::CaseInsensitive)&&(!cursor.block().text().contains(";",Qt::CaseInsensitive)))
{
ui->label->setText("Checking condition for If Statement");
}
else if(cursor.block().text().contains("else",Qt::CaseInsensitive)&& cursor.block().text().contains("if",Qt::CaseInsensitive))
{
ui->label->setText("Checking condition for If Statement");

}

else if(cursor.block().text().contains("else",Qt::CaseInsensitive))
{
ui->label->setText("Checking condition for Else Statement");

}
}

Can anybody help me?
I tried this code:


// For header file

#ifndef LABELTEXT_H
#define LABELTEXT_H


#include <QLabel>


class QLabel;

class Labeltext : public QLabel
{
Q_OBJECT

public:
Labeltext(QLabel *parent = 0);

protected:
void labtext();

private:

};

#endif // LABELTEXT_H


but in cpp file i dont know what to include in constructor?

anda_skoa
29th January 2014, 13:02
but in cpp file i dont know what to include in constructor?

You added the labtext() declaration to the wrong header.
You need to add it to the class MainWindow or move the implementation from MainWindow to class Labeltext,

Cheers,
_

parulkalra14
30th January 2014, 05:28
Yeah actually i had created this labtxt function in mainwindow but now i want to create separate cpp and header file only for this particular function labtext. So i have created header file for labtxt but i dont know what to add in constuctor of labeltext in its cpp file.

anda_skoa
30th January 2014, 08:52
I don't see any need to do something in the constructor of class Labeltext, just pass the parent argument to the base class constructor.

Cheers,
_

parulkalra14
30th January 2014, 11:10
ok...then labeltext.h header file will include:
#ifndef LABELTEXT_H
#define LABELTEXT_H
#include <QLabel>
#include<mainwindow.h>



class QLabel;

class Labeltext : public QPlainTextEdit
{
Q_OBJECT

public:
Labeltext(QLabel *parent = 0);

protected:
void labtext();

private:

};

#endif // LABELTEXT_H


and labeltext.cpp will contain:
#include "labeltext.h"
#include<mainwindow.h>
#include<QLabel>

Labeltext::Labeltext(QLabel *parent) :QPlainTextEdit(parent)
{

}

void labtext()
{
if(cursor.block().text().contains("",Qt::CaseInsensitive))
{
ui->label->setText("");

}

if(cursor.block().text().contains("printf",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive)||cursor.block().text().conta ins("cout",Qt::CaseInsensitive)&&cursor.block().text().contains("<<",Qt::CaseInsensitive))
{
ui->label->setText("Printing to Standard Output");
}
......//so on
}
then how to call in mainwindow.cpp?

sulliwk06
30th January 2014, 15:22
Well you'll have to create an instance of Labeltext in your main window, and then call the function from that. You'll have to include the labeltext header in mainwindow.

I don't mean to be rude, but are you sure you know enough about c++ to be making what appears to be and IDE/debugger for it?

anda_skoa
30th January 2014, 15:52
class Labeltext : public QPlainTextEdit


Are you sure your Labeltext class should be a QPlainTextEdit?



Labeltext(QLabel *parent = 0);

And you are sure you have a QLabel as its parent, not some container widget?

Cheers,
_

parulkalra14
3rd February 2014, 06:06
Label is contain in Frame in my GUI so parent of labeltext should QFrame only..
So Labeltext(QLabel *parent = 0); is replaced with Labeltext(QFrame *parent = 0);

and Class Labeltext should inherit properties from Class Label.
So class Labeltext : public QPlainTextEdit is with class Labeltext : public QLabel

Am i correct now?

anda_skoa
3rd February 2014, 11:09
Yes, that sounds a lot better

Cheers,
_

parulkalra14
4th February 2014, 05:44
So labeltext.cpp contain only one function void labtext(). So how to call this function from mainwindow.cpp?



#include "labeltext.h"
#include<QFrame>
#include<QLabel>

Labeltext::Labeltext(QFrame *parent) :QLabel(parent)
{

}

void labtext()
{
if(cursor.block().text().contains("",Qt::CaseInsensitive))
{
ui->label->setText("");

}

if(cursor.block().text().contains("printf",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive)||cursor.block().text().conta ins("cout",Qt::CaseInsensitive)&&cursor.block().text().contains("<<",Qt::CaseInsensitive))
{
ui->label->setText("Printing to Standard Output");
}

else if(cursor.block().text().contains("while",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive))
{
ui->label->setText("Checking condition for While Statement");

}
}

anda_skoa
4th February 2014, 09:21
So labeltext.cpp contain only one function void labtext(). So how to call this function from mainwindow.cpp?

I am not sure I understand the question, a method call is basic C++



pointerToLabelTextObject->labtext();


Cheers,
_

parulkalra14
4th February 2014, 11:07
Its not working showing Error undefined reference to Labeltext::labtext();
I declared class object in mainwindow.h like
Labeltext *labeltext;

and i called labtext() with the help of class Labeltext object i.e labeltext in this way :

labeltext->labtext()

anda_skoa
4th February 2014, 12:13
That is a linker error, so make sure that the code for labtext() is actually compiled. I.e. make sure that the respective source file is listed in SOURCES in your .pro file.

Cheers,
_

d_stranz
5th February 2014, 03:18
If this is what your .cpp file really looks like:


Labeltext::Labeltext(QFrame *parent) :QLabel(parent)
{

}

void labtext()
{
...
}


then you have not implemented the function labtext() as a member of the class Labeltext. You have implemented it as a standalone function inside a cpp file. Your linker is complaining that it can't find Labeltext::labtext() because in fact you declared it in the header, but didn't implement it.

I think you need to learn how to walk in C++ before you attempt to write code in C++ + Qt.

parulkalra14
5th February 2014, 06:27
ok i understand where i am going wrong...i replaced below code:
Labeltext::Labeltext(QFrame *parent) :QLabel(parent)
{

}

void labtext()
{
...
}

with

Labeltext::Labeltext(QFrame *parent) :QLabel(parent)
{

}

void Labeltext:: labtext()
{
QTextCursor cursor=QPlainTextEdit.textCursor();
if(cursor.block().text().contains("",Qt::CaseInsensitive))
{
Qlabel->setText("");

}
}
Something is wrong with this highlighted line and i am not able to find and my appication is stopped working when i run it. Need help!!!

anda_skoa
5th February 2014, 09:37
What do you expect this line to do?

Do you have a member variable called QPlainTextEdit? Is its type a class that has a textCursor() method?


Cheers,
_

parulkalra14
5th February 2014, 11:43
I am declaring cursor variable and want to link cursor with plainTextEdit so that whenever cursor moves on a text, the label associated with that particular text will displayed on a Label Widget.

sulliwk06
5th February 2014, 13:20
This thread is killing me. You really need to take a class or read a book on C++.

"QPlainTextEdit.textCursor()" is calling the function textCursor on an object named QPlainTextEdit.
What you want is to call the function textCursor on an object of type QPlainTextEdit.

So you should have a QPlainTextEdit declared somewhere

QPlainTextEdit myPlainTextEdit;
and then call textCursor on that

myPlainTextEdit.textCursor();

parulkalra14
6th February 2014, 06:19
Now, I also think so i have to read a book of C++ again but i think i was also not able to clear what exactly my problem is. Actually i have textEdit i.e already declare in mainwindow.h
as QPlainTextEdit *textEdit;
I want to link my new lebeltext.cpp with mainwindow.cpp and in mainwindow.cpp i have created a variable for PlaintextEdit in this way:
textEdit = ui->plainTextEdit;
So i dont want to create another plainTextEdit. I want labeltext.cpp invoke a textEdit i.e declare in mainwindow.h.

stampede
6th February 2014, 07:50
This thread is killing me. You really need to take a class or read a book on C++.
Agreed, I guess this is what happens when you start programming with Qt without any solid C++/OO experience.

I also think so i have to read a book of C++ again
No. You have to read it twice. And then read another. And then create some complex C++ applications without Qt GUI. In the meantime reading another thousand books about programming, algorithms, operating systems, object-oriented design, best programming practices etc...
Trying to create a full-blown IDE with debugging/step-by-step execution possibilities is definitely not a good idea, considering the kind of "problems" you are posting on this forum.


I want labeltext.cpp invoke a textEdit i.e declare in mainwindow.h.
Can you explain why a label should have access to internals of MainWindow ? I think you only want to display a text in a label, so just pass this text to the label instance. You need to reverse the dependency (label accessing mainWindow => bad, mainWindow using a label => good).
If you read that C++ book, you should know that encapsulation (https://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming))is a very important aspect of object-oriented design (https://en.wikipedia.org/wiki/Object-oriented_design).

anda_skoa
6th February 2014, 09:01
I want to link my new lebeltext.cpp with mainwindow.cpp

You want to pass the text edit pointer to the label, e.g. in the constructor or in a setter.

In general I'd have to agree with stampede that you should probably try something simpler first, get to know the language and surrounding concepts.
Accessing one object from another is so basic that you shouldn't even have to think about it.

Cheers,
_