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
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

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

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

    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.

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

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

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

    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?

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

    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?

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

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

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

    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?

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

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

    Yes, that sounds a lot better

    Cheers,
    _

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

    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

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

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

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

    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.

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

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

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    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.

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.