Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Facing problem in creating new cpp and Header file

  1. #1
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Facing problem in creating new cpp and Header file

    I want to create cpp and header file for this function.
    Qt Code:
    1. void MainWindow::labtext()//labeltext function
    2. {
    3. if(cursor.block().text().contains("",Qt::CaseInsensitive))
    4. {
    5. ui->label->setText("");
    6.  
    7. }
    8.  
    9.  
    10. if(cursor.block().text().contains("printf",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive)||cursor.block().text().contains("cout",Qt::CaseInsensitive)&&cursor.block().text().contains("<<",Qt::CaseInsensitive))
    11. {
    12. ui->label->setText("Printing to Standard Output");
    13. }
    14.  
    15. else if(cursor.block().text().contains("while",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive))
    16. {
    17. ui->label->setText("Checking condition for While Statement");
    18. //QMessageBox::critical(this,"oo","fdgffh");
    19. }
    20.  
    21.  
    22.  
    23.  
    24. else if(cursor.block().text().contains("switch",Qt::CaseInsensitive)&&cursor.block().text().contains("(",Qt::CaseInsensitive))
    25. {
    26. ui->label->setText("Checking condition for Switch Statement");
    27. }
    28. else if(cursor.block().text().contains("if",Qt::CaseInsensitive)&&cursor.block().text().contains("(",Qt::CaseInsensitive)&&(!cursor.block().text().contains(";",Qt::CaseInsensitive)))
    29. {
    30. ui->label->setText("Checking condition for If Statement");
    31. }
    32. else if(cursor.block().text().contains("else",Qt::CaseInsensitive)&& cursor.block().text().contains("if",Qt::CaseInsensitive))
    33. {
    34. ui->label->setText("Checking condition for If Statement");
    35.  
    36. }
    37.  
    38. else if(cursor.block().text().contains("else",Qt::CaseInsensitive))
    39. {
    40. ui->label->setText("Checking condition for Else Statement");
    41.  
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 
    Can anybody help me?
    I tried this code:
    Qt Code:
    1. // For header file
    2.  
    3. #ifndef LABELTEXT_H
    4. #define LABELTEXT_H
    5.  
    6.  
    7. #include <QLabel>
    8.  
    9.  
    10. class QLabel;
    11.  
    12. class Labeltext : public QLabel
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Labeltext(QLabel *parent = 0);
    18.  
    19. protected:
    20. void labtext();
    21.  
    22. private:
    23.  
    24. };
    25.  
    26. #endif // LABELTEXT_H
    To copy to clipboard, switch view to plain text mode 

    but in cpp file i dont know what to include in constructor?
    Last edited by anda_skoa; 29th January 2014 at 12:59. Reason: add code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    Quote Originally Posted by parulkalra14 View Post
    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,
    _

  3. #3
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    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,
    _

  5. #5
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    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::CaseInsen sitive))
    {
    ui->label->setText("");

    }

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

  6. #6
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    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?

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    Quote Originally Posted by parulkalra14 View Post
    class Labeltext : public QPlainTextEdit
    Are you sure your Labeltext class should be a QPlainTextEdit?

    Quote Originally Posted by parulkalra14 View Post
    Labeltext(QLabel *parent = 0);
    And you are sure you have a QLabel as its parent, not some container widget?

    Cheers,
    _

  8. #8
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    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?

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    Yes, that sounds a lot better

    Cheers,
    _

  10. #10
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    So labeltext.cpp contain only one function void labtext(). So how to call this function from mainwindow.cpp?

    Qt Code:
    1. #include "labeltext.h"
    2. #include<QFrame>
    3. #include<QLabel>
    4.  
    5. Labeltext::Labeltext(QFrame *parent) :QLabel(parent)
    6. {
    7.  
    8. }
    9.  
    10. void labtext()
    11. {
    12. if(cursor.block().text().contains("",Qt::CaseInsensitive))
    13. {
    14. ui->label->setText("");
    15.  
    16. }
    17.  
    18. if(cursor.block().text().contains("printf",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive)||cursor.block().text().contains("cout",Qt::CaseInsensitive)&&cursor.block().text().contains("<<",Qt::CaseInsensitive))
    19. {
    20. ui->label->setText("Printing to Standard Output");
    21. }
    22.  
    23. else if(cursor.block().text().contains("while",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive))
    24. {
    25. ui->label->setText("Checking condition for While Statement");
    26.  
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 4th February 2014 at 09:21. Reason: Added code tags

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    Quote Originally Posted by parulkalra14 View Post
    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++

    Qt Code:
    1. pointerToLabelTextObject->labtext();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  12. #12
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    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()
    Last edited by parulkalra14; 4th February 2014 at 11:30.

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    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,
    _

  14. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,313
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    If this is what your .cpp file really looks like:
    Qt Code:
    1. Labeltext::Labeltext(QFrame *parent) :QLabel(parent)
    2. {
    3.  
    4. }
    5.  
    6. void labtext()
    7. {
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    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.

  15. #15
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    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::CaseInsen sitive))
    {
    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!!!
    Last edited by parulkalra14; 5th February 2014 at 07:36.

  16. #16
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    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,
    _

  17. #17
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    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.

  18. #18
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Facing problem in creating new cpp and Header file

    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
    Qt Code:
    1. QPlainTextEdit myPlainTextEdit;
    To copy to clipboard, switch view to plain text mode 
    and then call textCursor on that
    Qt Code:
    1. myPlainTextEdit.textCursor();
    To copy to clipboard, switch view to plain text mode 

  19. The following 2 users say thank you to sulliwk06 for this useful post:

    anda_skoa (5th February 2014), d_stranz (5th February 2014)

  20. #19
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Facing problem in creating new cpp and Header file

    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.

  21. #20
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Facing problem in creating new cpp and Header file

    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 is a very important aspect of object-oriented design.

Similar Threads

  1. Qt Creator PROBLEM INCLUDING 'QSystemInfo' HEADER FILE
    By Rakula in forum Qt Tools
    Replies: 1
    Last Post: 24th September 2010, 09:28
  2. I am facing a problem please help me !
    By sujan.dasmahapatra in forum General Programming
    Replies: 3
    Last Post: 16th September 2009, 14:12
  3. 'QFile' Header File Problem
    By hasnatzaidi in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 21:40
  4. Facing problem with Q3Canvas
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 6th May 2006, 07:00
  5. Facing problem with qt-4.1 designer
    By jnana in forum Qt Tools
    Replies: 4
    Last Post: 8th March 2006, 18:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.