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. How to reach any class members from Mainwindow class?
Qt 6.11 is out! See what's new in the release blog

How to reach any class members from Mainwindow class?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 409 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.
  • E Offline
    E Offline
    ELIF
    wrote on last edited by
    #1

    I have a QT Widget application. I
    I want to reach ismet class members from mainwindow.cpp file
    In mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
    
         is=new ismet();
         is->My_Func(2);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    In mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    
    #include <QMainWindow>
    #include "ismet.h"
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
         ismet *is;
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    In ismet.cpp

    
    
    ismet::ismet()
    {
        My_Func(2);
    }
    
    
    void ismet::My_Func(int x){
        qDebug()<<x;
    
    }
    

    In ismet.h

    #ifndef ISMET_H
    #define ISMET_H
    
    #include<QDebug>
    #include<QObject>
    #include<QString>
    #include<QMainWindow>
    
    class ismet : public QObject
    {
    public:
        ismet();
    
        void My_Func(int x);//declaration
        QString myname;
    };
    
    #endif // ISMET_H
    
    

    My error is : error: undefined reference to ismet::ismet()' undefined reference to ismet::My_Func(int)'

    How to solve this issue?

    1 Reply Last reply
    0
    • kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #2

      Please post your build system configuration (CMakeLists.txt file , or .pro file). Are you actually compiling and linking ismet.cpp into the executable?

      Director R&D, The Qt Company

      1 Reply Last reply
      1
      • E Offline
        E Offline
        ELIF
        wrote on last edited by
        #3
        #-------------------------------------------------
        #
        # Project created by QtCreator 2022-06-27T14:19:26
        #
        #-------------------------------------------------
        
        QT       += core gui
        
        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
        
        TARGET = calisma
        TEMPLATE = app
        
        # The following define makes your compiler emit warnings if you use
        # any feature of Qt which has been marked as deprecated (the exact warnings
        # depend on your compiler). Please consult the documentation of the
        # deprecated API in order to know how to port your code away from it.
        DEFINES += QT_DEPRECATED_WARNINGS
        
        # You can also make your code fail to compile if you use deprecated APIs.
        # In order to do so, uncomment the following line.
        # You can also select to disable deprecated APIs only up to a certain version of Qt.
        #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
        
        
        SOURCES += \
                main.cpp \
                mainwindow.cpp \
            ismet.cpp
        
        HEADERS += \
                mainwindow.h \
            ismet.h
        
        FORMS += \
                mainwindow.ui
        
        
        jsulmJ 1 Reply Last reply
        0
        • kkoehneK Offline
          kkoehneK Offline
          kkoehne
          Moderators
          wrote on last edited by
          #4

          This looks correct to me. Can you also post the full compiler output (not just the error message, but everything in Qt Creator under 'Compile Output')?

          Director R&D, The Qt Company

          1 Reply Last reply
          0
          • E ELIF
            #-------------------------------------------------
            #
            # Project created by QtCreator 2022-06-27T14:19:26
            #
            #-------------------------------------------------
            
            QT       += core gui
            
            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
            
            TARGET = calisma
            TEMPLATE = app
            
            # The following define makes your compiler emit warnings if you use
            # any feature of Qt which has been marked as deprecated (the exact warnings
            # depend on your compiler). Please consult the documentation of the
            # deprecated API in order to know how to port your code away from it.
            DEFINES += QT_DEPRECATED_WARNINGS
            
            # You can also make your code fail to compile if you use deprecated APIs.
            # In order to do so, uncomment the following line.
            # You can also select to disable deprecated APIs only up to a certain version of Qt.
            #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
            
            
            SOURCES += \
                    main.cpp \
                    mainwindow.cpp \
                ismet.cpp
            
            HEADERS += \
                    mainwindow.h \
                ismet.h
            
            FORMS += \
                    mainwindow.ui
            
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @ELIF You can also try complete rebuild:

            1. Delete build folder
            2. Run qmake
            3. Build

            Maybe qmake was not executed after addin ismet.cpp to pro file.

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

            1 Reply Last reply
            2

            • Login

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