Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. creating cpp library using qt
QtWS25 Last Chance

creating cpp library using qt

Scheduled Pinned Locked Moved General and Desktop
23 Posts 3 Posters 7.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jsulmJ jsulm

    @amruz said in creating cpp library using qt:

    but when i tried to use the library in my application it is showing like qpushbutton cannot be found..qlabel not found..

    That indicates that you're forgot to link your app against needed Qt libraries. Or even header files cannot be found? Can you show your pro file? And the exact errors? Else we can just guess what is the problem

    A Offline
    A Offline
    amruz
    wrote on last edited by amruz
    #5
    This post is deleted!
    1 Reply Last reply
    0
    • jsulmJ jsulm

      @amruz said in creating cpp library using qt:

      but when i tried to use the library in my application it is showing like qpushbutton cannot be found..qlabel not found..

      That indicates that you're forgot to link your app against needed Qt libraries. Or even header files cannot be found? Can you show your pro file? And the exact errors? Else we can just guess what is the problem

      A Offline
      A Offline
      amruz
      wrote on last edited by amruz
      #6

      @jsulm
      sharedlib.pro

      #-------------------------------------------------
      #
      # Project created by QtCreator 2017-07-27T15:45:22
      #
      #-------------------------------------------------
      
      QT       += widgets
      
      QT       -= core gui
      
      TARGET = worksharedlib
      TEMPLATE = lib
      
      DEFINES += WORKSHAREDLIB_LIBRARY
      
      SOURCES += worksharedlib.cpp
      
      HEADERS += worksharedlib.h\
              worksharedlib_global.h
      
      unix {
          target.path = /usr/lib
          INSTALLS += target
      }
      
      

      sharedlib.h

      #ifndef WORKSHAREDLIB_H
      #define WORKSHAREDLIB_H
      
      #include "worksharedlib_global.h"
      #include<qobject.h>
      #include<qwidget.h>
      #include <QPushButton>
      #include<QLineEdit>
      #include <QHBoxLayout>
      #include<qlabel.h>
      #include<QtGui>
      
      
      class WORKSHAREDLIBSHARED_EXPORT Worksharedlib:public QWidget
      {
      
         Q_OBJECT
      public:
          //Worksharedlib();
          Worksharedlib(QWidget *parent = 0);
          ~Worksharedlib() {}
      
      private slots:
      
         void handleButton();
      
         void m_buttonClicked();
      
      private:
      
         QPushButton *m_button;
      
         QLineEdit *m_textbox;
      
         QHBoxLayout *layout;
      
         QWidget *window;
      
         QLabel * label;
      
         QWidget *frame;
      
      };
      
      #endif // WORKSHAREDLIB_H
      
      

      sharedlib_global.h

      #ifndef WORKSHAREDLIB_GLOBAL_H
      #define WORKSHAREDLIB_GLOBAL_H
      
      #include <QtCore/qglobal.h>
      
      #if defined(WORKSHAREDLIB_LIBRARY)
      #  define WORKSHAREDLIBSHARED_EXPORT Q_DECL_EXPORT
      #else
      #  define WORKSHAREDLIBSHARED_EXPORT Q_DECL_IMPORT
      #endif
      
      #endif // WORKSHAREDLIB_GLOBAL_H
      
      

      sharedlib.cpp

      #include "worksharedlib.h"
      
      #include <QCoreApplication>
      #include <QMessageBox>
      #include <QDebug>
      using namespace std;
      
      Worksharedlib::Worksharedlib(QWidget *parent)
                                   : QWidget (parent)
      {
              layout = new QHBoxLayout( this );
              m_textbox = new QLineEdit ( this );
              // Create the button, make "this" the parent
              m_button = new QPushButton("My Button",this);
              // set size and location of the button
              m_button->setGeometry(QRect(QPoint(300, 200),
                                          QSize(200, 50)));
      
              //create the text box
              layout->addWidget(m_textbox);
              m_textbox->setGeometry(100,200,90,25);
      
      
              //try image
      
      
      
              //create a image viewer
              QString url = R"(/home/pi/krishna.jpg)";
              QPixmap img(url);
              QLabel *label = new QLabel(this);
              label->setGeometry(200,300,100,50);
              label->resize(this->width(),300);
              label->setPixmap(img);
              // Connect button signal to appropriate slot
              connect(m_button, SIGNAL (clicked()), this, SLOT (m_buttonClicked()));
      }
      
      void Worksharedlib::m_buttonClicked()
      {
          m_textbox = new QLineEdit;
          m_textbox->setPlaceholderText("Placeholder Text");
          m_textbox->setFocus();
          QMessageBox::information( this, "Information", "Just clicked Ui PushButton" );
      }
      
      
      

      in which all qt applications can i call this library.. what is the correct method to call this library in an application..

      jsulmJ 2 Replies Last reply
      0
      • A amruz

        @jsulm
        sharedlib.pro

        #-------------------------------------------------
        #
        # Project created by QtCreator 2017-07-27T15:45:22
        #
        #-------------------------------------------------
        
        QT       += widgets
        
        QT       -= core gui
        
        TARGET = worksharedlib
        TEMPLATE = lib
        
        DEFINES += WORKSHAREDLIB_LIBRARY
        
        SOURCES += worksharedlib.cpp
        
        HEADERS += worksharedlib.h\
                worksharedlib_global.h
        
        unix {
            target.path = /usr/lib
            INSTALLS += target
        }
        
        

        sharedlib.h

        #ifndef WORKSHAREDLIB_H
        #define WORKSHAREDLIB_H
        
        #include "worksharedlib_global.h"
        #include<qobject.h>
        #include<qwidget.h>
        #include <QPushButton>
        #include<QLineEdit>
        #include <QHBoxLayout>
        #include<qlabel.h>
        #include<QtGui>
        
        
        class WORKSHAREDLIBSHARED_EXPORT Worksharedlib:public QWidget
        {
        
           Q_OBJECT
        public:
            //Worksharedlib();
            Worksharedlib(QWidget *parent = 0);
            ~Worksharedlib() {}
        
        private slots:
        
           void handleButton();
        
           void m_buttonClicked();
        
        private:
        
           QPushButton *m_button;
        
           QLineEdit *m_textbox;
        
           QHBoxLayout *layout;
        
           QWidget *window;
        
           QLabel * label;
        
           QWidget *frame;
        
        };
        
        #endif // WORKSHAREDLIB_H
        
        

        sharedlib_global.h

        #ifndef WORKSHAREDLIB_GLOBAL_H
        #define WORKSHAREDLIB_GLOBAL_H
        
        #include <QtCore/qglobal.h>
        
        #if defined(WORKSHAREDLIB_LIBRARY)
        #  define WORKSHAREDLIBSHARED_EXPORT Q_DECL_EXPORT
        #else
        #  define WORKSHAREDLIBSHARED_EXPORT Q_DECL_IMPORT
        #endif
        
        #endif // WORKSHAREDLIB_GLOBAL_H
        
        

        sharedlib.cpp

        #include "worksharedlib.h"
        
        #include <QCoreApplication>
        #include <QMessageBox>
        #include <QDebug>
        using namespace std;
        
        Worksharedlib::Worksharedlib(QWidget *parent)
                                     : QWidget (parent)
        {
                layout = new QHBoxLayout( this );
                m_textbox = new QLineEdit ( this );
                // Create the button, make "this" the parent
                m_button = new QPushButton("My Button",this);
                // set size and location of the button
                m_button->setGeometry(QRect(QPoint(300, 200),
                                            QSize(200, 50)));
        
                //create the text box
                layout->addWidget(m_textbox);
                m_textbox->setGeometry(100,200,90,25);
        
        
                //try image
        
        
        
                //create a image viewer
                QString url = R"(/home/pi/krishna.jpg)";
                QPixmap img(url);
                QLabel *label = new QLabel(this);
                label->setGeometry(200,300,100,50);
                label->resize(this->width(),300);
                label->setPixmap(img);
                // Connect button signal to appropriate slot
                connect(m_button, SIGNAL (clicked()), this, SLOT (m_buttonClicked()));
        }
        
        void Worksharedlib::m_buttonClicked()
        {
            m_textbox = new QLineEdit;
            m_textbox->setPlaceholderText("Placeholder Text");
            m_textbox->setFocus();
            QMessageBox::information( this, "Information", "Just clicked Ui PushButton" );
        }
        
        
        

        in which all qt applications can i call this library.. what is the correct method to call this library in an application..

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @amruz This is your shared lib, I'm talking about your app which uses the lib.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply
        0
        • A amruz

          @jsulm
          sharedlib.pro

          #-------------------------------------------------
          #
          # Project created by QtCreator 2017-07-27T15:45:22
          #
          #-------------------------------------------------
          
          QT       += widgets
          
          QT       -= core gui
          
          TARGET = worksharedlib
          TEMPLATE = lib
          
          DEFINES += WORKSHAREDLIB_LIBRARY
          
          SOURCES += worksharedlib.cpp
          
          HEADERS += worksharedlib.h\
                  worksharedlib_global.h
          
          unix {
              target.path = /usr/lib
              INSTALLS += target
          }
          
          

          sharedlib.h

          #ifndef WORKSHAREDLIB_H
          #define WORKSHAREDLIB_H
          
          #include "worksharedlib_global.h"
          #include<qobject.h>
          #include<qwidget.h>
          #include <QPushButton>
          #include<QLineEdit>
          #include <QHBoxLayout>
          #include<qlabel.h>
          #include<QtGui>
          
          
          class WORKSHAREDLIBSHARED_EXPORT Worksharedlib:public QWidget
          {
          
             Q_OBJECT
          public:
              //Worksharedlib();
              Worksharedlib(QWidget *parent = 0);
              ~Worksharedlib() {}
          
          private slots:
          
             void handleButton();
          
             void m_buttonClicked();
          
          private:
          
             QPushButton *m_button;
          
             QLineEdit *m_textbox;
          
             QHBoxLayout *layout;
          
             QWidget *window;
          
             QLabel * label;
          
             QWidget *frame;
          
          };
          
          #endif // WORKSHAREDLIB_H
          
          

          sharedlib_global.h

          #ifndef WORKSHAREDLIB_GLOBAL_H
          #define WORKSHAREDLIB_GLOBAL_H
          
          #include <QtCore/qglobal.h>
          
          #if defined(WORKSHAREDLIB_LIBRARY)
          #  define WORKSHAREDLIBSHARED_EXPORT Q_DECL_EXPORT
          #else
          #  define WORKSHAREDLIBSHARED_EXPORT Q_DECL_IMPORT
          #endif
          
          #endif // WORKSHAREDLIB_GLOBAL_H
          
          

          sharedlib.cpp

          #include "worksharedlib.h"
          
          #include <QCoreApplication>
          #include <QMessageBox>
          #include <QDebug>
          using namespace std;
          
          Worksharedlib::Worksharedlib(QWidget *parent)
                                       : QWidget (parent)
          {
                  layout = new QHBoxLayout( this );
                  m_textbox = new QLineEdit ( this );
                  // Create the button, make "this" the parent
                  m_button = new QPushButton("My Button",this);
                  // set size and location of the button
                  m_button->setGeometry(QRect(QPoint(300, 200),
                                              QSize(200, 50)));
          
                  //create the text box
                  layout->addWidget(m_textbox);
                  m_textbox->setGeometry(100,200,90,25);
          
          
                  //try image
          
          
          
                  //create a image viewer
                  QString url = R"(/home/pi/krishna.jpg)";
                  QPixmap img(url);
                  QLabel *label = new QLabel(this);
                  label->setGeometry(200,300,100,50);
                  label->resize(this->width(),300);
                  label->setPixmap(img);
                  // Connect button signal to appropriate slot
                  connect(m_button, SIGNAL (clicked()), this, SLOT (m_buttonClicked()));
          }
          
          void Worksharedlib::m_buttonClicked()
          {
              m_textbox = new QLineEdit;
              m_textbox->setPlaceholderText("Placeholder Text");
              m_textbox->setFocus();
              QMessageBox::information( this, "Information", "Just clicked Ui PushButton" );
          }
          
          
          

          in which all qt applications can i call this library.. what is the correct method to call this library in an application..

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @amruz said in creating cpp library using qt:

          in which all qt applications can i call this library.. what is the correct method to call this library in an application..

          You just link against your library and Qt in your app.
          See here http://doc.qt.io/qtcreator/creator-project-qmake-libraries.html

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @amruz This is your shared lib, I'm talking about your app which uses the lib.

            A Offline
            A Offline
            amruz
            wrote on last edited by amruz
            #9

            @jsulm the pro file of my application which calls the library is

            QT += core
            QT -= gui
            
            CONFIG += c++11
            
            TARGET = testcretedlibrary
            CONFIG += console
            CONFIG -= app_bundle
            
            TEMPLATE = app
            
            SOURCES += main.cpp
            
            DEPENDPATH += /home/amruz/worksharedlib
            INCLUDEPATH += /home/amruz/worksharedlib
            LIBS += -L /home/amruz/build-worksharedlib-Desktop_Qt_5_7_0_GCC_64bit-Debug -lworksharedlib
            
            
            jsulmJ 1 Reply Last reply
            0
            • A amruz

              @jsulm the pro file of my application which calls the library is

              QT += core
              QT -= gui
              
              CONFIG += c++11
              
              TARGET = testcretedlibrary
              CONFIG += console
              CONFIG -= app_bundle
              
              TEMPLATE = app
              
              SOURCES += main.cpp
              
              DEPENDPATH += /home/amruz/worksharedlib
              INCLUDEPATH += /home/amruz/worksharedlib
              LIBS += -L /home/amruz/build-worksharedlib-Desktop_Qt_5_7_0_GCC_64bit-Debug -lworksharedlib
              
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @amruz Add

              QT       += widgets
              
              

              Fix the LIBS line (no space between -L and the path):

              LIBS += -L/home/amruz/build-worksharedlib-Desktop_Qt_5_7_0_GCC_64bit-Debug -lworksharedlib
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply
              0
              • jsulmJ jsulm

                @amruz Add

                QT       += widgets
                
                

                Fix the LIBS line (no space between -L and the path):

                LIBS += -L/home/amruz/build-worksharedlib-Desktop_Qt_5_7_0_GCC_64bit-Debug -lworksharedlib
                
                A Offline
                A Offline
                amruz
                wrote on last edited by
                #11

                @jsulm said in creating cpp library using qt:

                QT += widgets

                can you also help me in writing the main.cpp file..
                how to actually call the mainwindow there with the library output in it

                jsulmJ 1 Reply Last reply
                0
                • A amruz

                  @jsulm said in creating cpp library using qt:

                  QT += widgets

                  can you also help me in writing the main.cpp file..
                  how to actually call the mainwindow there with the library output in it

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  @amruz You use it as any other library. You're already doing it with Qt.
                  Just include the header file of your library and use it. For example create an instance of Worksharedlib.

                  #include "sharedlib.h"
                  
                  int main()
                  {
                      ...
                      Worksharedlib lib;
                      lib.show();
                      ...
                  }

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  A 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @amruz You use it as any other library. You're already doing it with Qt.
                    Just include the header file of your library and use it. For example create an instance of Worksharedlib.

                    #include "sharedlib.h"
                    
                    int main()
                    {
                        ...
                        Worksharedlib lib;
                        lib.show();
                        ...
                    }
                    A Offline
                    A Offline
                    amruz
                    wrote on last edited by
                    #13

                    @jsulm said in creating cpp library using qt:

                    Worksharedlib lib;
                    lib.show();

                    i used the same way only.. but i am getting error as follows

                    /home/amruz/worksharedlib/worksharedlib.h:14: error: undefined reference to `vtable for Worksharedlib'

                    jsulmJ 1 Reply Last reply
                    0
                    • A amruz

                      @jsulm said in creating cpp library using qt:

                      Worksharedlib lib;
                      lib.show();

                      i used the same way only.. but i am getting error as follows

                      /home/amruz/worksharedlib/worksharedlib.h:14: error: undefined reference to `vtable for Worksharedlib'

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      @amruz Will probably not solve that error, but this looks wrong as well:

                      QT -= gui
                      

                      Your app is going to show an UI, right? Then it is a gui app:

                      QT += core gui widgets
                      

                      Don't forget to rerun qmake after changing pro file and rebuild.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      A 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @amruz Will probably not solve that error, but this looks wrong as well:

                        QT -= gui
                        

                        Your app is going to show an UI, right? Then it is a gui app:

                        QT += core gui widgets
                        

                        Don't forget to rerun qmake after changing pro file and rebuild.

                        A Offline
                        A Offline
                        amruz
                        wrote on last edited by
                        #15

                        @jsulm said in creating cpp library using qt:

                        QT += core gui widgets

                        this is a console application actually..is that the problem?

                        jsulmJ 1 Reply Last reply
                        0
                        • A amruz

                          @jsulm said in creating cpp library using qt:

                          QT += core gui widgets

                          this is a console application actually..is that the problem?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          @amruz If it is a console application then why do you want to use a library which provides an UI class:

                          class WORKSHAREDLIBSHARED_EXPORT Worksharedlib:public QWidget
                          {
                          
                             Q_OBJECT
                          public:
                              //Worksharedlib();
                              Worksharedlib(QWidget *parent = 0);
                              ~Worksharedlib() {}
                          
                          private slots:
                             void handleButton();
                             void m_buttonClicked();
                          
                          private:
                             QPushButton *m_button;
                             QLineEdit *m_textbox;
                             QHBoxLayout *layout;
                             QWidget *window;
                             QLabel * label;
                             QWidget *frame;
                          };
                          

                          ?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          A 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @amruz If it is a console application then why do you want to use a library which provides an UI class:

                            class WORKSHAREDLIBSHARED_EXPORT Worksharedlib:public QWidget
                            {
                            
                               Q_OBJECT
                            public:
                                //Worksharedlib();
                                Worksharedlib(QWidget *parent = 0);
                                ~Worksharedlib() {}
                            
                            private slots:
                               void handleButton();
                               void m_buttonClicked();
                            
                            private:
                               QPushButton *m_button;
                               QLineEdit *m_textbox;
                               QHBoxLayout *layout;
                               QWidget *window;
                               QLabel * label;
                               QWidget *frame;
                            };
                            

                            ?

                            A Offline
                            A Offline
                            amruz
                            wrote on last edited by
                            #17

                            @jsulm which application should i use to test this library?

                            jsulmJ 1 Reply Last reply
                            0
                            • A amruz

                              @jsulm which application should i use to test this library?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #18

                              @amruz gui?

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              A 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @amruz gui?

                                A Offline
                                A Offline
                                amruz
                                wrote on last edited by
                                #19

                                @jsulm do you mean qt widget application?

                                jsulmJ 1 Reply Last reply
                                0
                                • A amruz

                                  @jsulm do you mean qt widget application?

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #20

                                  @amruz Yes. You're using widgets - so it must be a gui application with widgets, right?

                                  QT += core gui widgets
                                  

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  A 2 Replies Last reply
                                  0
                                  • jsulmJ jsulm

                                    @amruz Yes. You're using widgets - so it must be a gui application with widgets, right?

                                    QT += core gui widgets
                                    
                                    A Offline
                                    A Offline
                                    amruz
                                    wrote on last edited by
                                    #21

                                    @jsulm when i try to include this library in a widget application also i am getting the same error..

                                    this is the .pro file of my widgetapplication

                                    #-------------------------------------------------
                                    #
                                    # Project created by QtCreator 2017-07-27T17:19:21
                                    #
                                    #-------------------------------------------------
                                    
                                    QT       += core gui widgets
                                    
                                    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                                    
                                    TARGET = sharedlibtryinwidget
                                    TEMPLATE = app
                                    
                                    
                                    SOURCES += main.cpp\
                                            mainwindow.cpp
                                    
                                    HEADERS  += mainwindow.h
                                    
                                    DEPENDPATH += /home/amruz/worksharedlib
                                    INCLUDEPATH += /home/amruz/worksharedlib
                                    LIBS += -L/home/amruz/build-worksharedlib-Desktop_Qt_5_7_0_GCC_64bit-Debug -lworksharedlib
                                    
                                    
                                    
                                    A 1 Reply Last reply
                                    0
                                    • A amruz

                                      @jsulm when i try to include this library in a widget application also i am getting the same error..

                                      this is the .pro file of my widgetapplication

                                      #-------------------------------------------------
                                      #
                                      # Project created by QtCreator 2017-07-27T17:19:21
                                      #
                                      #-------------------------------------------------
                                      
                                      QT       += core gui widgets
                                      
                                      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                                      
                                      TARGET = sharedlibtryinwidget
                                      TEMPLATE = app
                                      
                                      
                                      SOURCES += main.cpp\
                                              mainwindow.cpp
                                      
                                      HEADERS  += mainwindow.h
                                      
                                      DEPENDPATH += /home/amruz/worksharedlib
                                      INCLUDEPATH += /home/amruz/worksharedlib
                                      LIBS += -L/home/amruz/build-worksharedlib-Desktop_Qt_5_7_0_GCC_64bit-Debug -lworksharedlib
                                      
                                      
                                      
                                      A Offline
                                      A Offline
                                      amruz
                                      wrote on last edited by
                                      #22

                                      @amruz i also found that when destructor is not included this type of error may occur..but when i added destructor also the same problem exists

                                      1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @amruz Yes. You're using widgets - so it must be a gui application with widgets, right?

                                        QT += core gui widgets
                                        
                                        A Offline
                                        A Offline
                                        amruz
                                        wrote on last edited by amruz
                                        #23

                                        @jsulm it worked finally! thanks a lot for all the help

                                        the problem was qobject in the library , i removed it and it worked.

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved