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. [SOLVED] problem with two classes in the same file
Forum Update on Monday, May 27th 2025

[SOLVED] problem with two classes in the same file

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 6.2k 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.
  • K Offline
    K Offline
    kalster
    wrote on 15 Nov 2011, 06:57 last edited by
    #1

    i created a pushbutton in the mainwindow.ui file. in the mainwindow.cpp file, the mainwindow functions, i can access the pushbutton methods. yet in the same mainwindow.cpp file, in the server functions, i can't access the pushbutton methods . i am getting the error in line 54, no matching function for call to 'Ui::MainWindow::setupUi(server* const)'. without the ui->setupUi(this); at line 55, the program crashes. below is the .h and cpp files.

    @
    ---------- mainwindow.h file

    namespace Ui {
    class server;
    }

    namespace Ui {
    class MainWindow;
    }

    class server : public QTcpServer
    {
    Q_OBJECT

    public:
        server(QObject *parent=0);
    
    private:
        Ui::MainWindow *ui;
    

    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    };

    -------------- mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    server::server(QObject *parent) :
    QTcpServer(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->textEdit->append("hi");
    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 15 Nov 2011, 07:13 last edited by
      #2

      Seems logical to me. You can only setup a ui on a widget, or in this case, on a QMainWindow. QTcpServer is not a QWidget, but a QObject.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on 15 Nov 2011, 07:22 last edited by
        #3

        thanks Andre

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kalster
          wrote on 16 Nov 2011, 06:29 last edited by
          #4

          now that i have two classes in the same file, how to a call the MainWindow function from a server function.

          for example, in the server::server function, i would like to call a function from the MainWindow class. for example...

          @void server::server(){
          MainWindow::Test();
          }@

          but the code above gives an error.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 16 Nov 2011, 07:09 last edited by
            #5

            "Gives an error" is not very descriptive. You need to post exactly which error you get.

            My guess: Test is not a static method of MainWindow, and thus can not be called on the class (like you do), but needs to be called on an instance of that class. So, you need to make sure that your server class gets a pointer to the mainwindow instance that you created.

            Or, better yet, you read up on the signals and slots mechanism in Qt. That would IMHO be a much better way to communicate between the server and the mainwindow. That way, they do not need pointers to each other, and can be developed independently.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kalster
              wrote on 16 Nov 2011, 07:19 last edited by
              #6

              i am getting the error: cannot declare member function 'static void MainWindow::test()' to have static linkage.

              i would rather not use signal and slot just yet as that would require about 100+ of them in the program.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BilbonSacquet
                wrote on 16 Nov 2011, 08:19 last edited by
                #7

                why did you not simply give to the server a main window pointer:
                @
                class server : public QTcpServer
                {
                Q_OBJECT

                public:
                    server(QObject *parent=0);
                
                private:
                    MainWindow *mw;
                

                };

                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this);

                server = new Ui::Server(this);
                

                }

                MainWindow::~MainWindow()
                {
                delete server;
                delete ui;
                }

                server::server(MainWindow *parent) :
                QTcpServer(parent),
                mw(parent)
                {
                mw->textEdit->append("hi");
                }

                server::Test()
                {
                mw->Test();
                }
                @

                If you want to call back the main window use simply the mw member.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on 16 Nov 2011, 08:25 last edited by
                  #8

                  [quote author="kalster" date="1321427999"]i am getting the error: cannot declare member function 'static void MainWindow::test()' to have static linkage.

                  i would rather not use signal and slot just yet as that would require about 100+ of them in the program.[/quote]

                  static functions != static linkage

                  static functions in C++ are functions that do not access member variables and that can be used due the class scope, not only on an instance.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalster
                    wrote on 17 Nov 2011, 06:56 last edited by
                    #9

                    [quote author="BilbonSacquet" date="1321431599"]why did you not simply give to the server a main window pointer:[/quote]

                    I think what Andre was saying in the first reply was that the mainwindow is a qwidget and server is an qobject. therefore i get this error when running your code. error: cannot convert 'QObject*' to 'MainWindow*' in initialization.

                    I have decided that i am going to use signal and slot. thank you

                    1 Reply Last reply
                    0

                    1/9

                    15 Nov 2011, 06:57

                    • Login

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