Results 1 to 20 of 21

Thread: Facing problem in creating new cpp and Header file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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,
    _

  2. #2
    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?

  3. #3
    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,
    _

  4. #4
    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

  5. #5
    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,
    _

  6. #6
    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.

  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

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

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    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.

  9. #9
    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.

  10. #10
    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,
    _

  11. #11
    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.

  12. #12
    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 

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

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

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.