[Solved] Splitting mainwindow.cpp into multiple files



  • Hello,

    I come from PHP background and last year I wrote a desktop program for displaying serial data in various graphics.

    I can barrely handling c++ and I can produce very good result so everyone happy except me.

    My mainwindow.cpp is getting bigger and bigger. Applying patches, making changes getting hard.

    So I'm going to build new version and I want to split mainwindow.cpp file in to multiple file like
    mainwindow.cpp
    mainwindow_actions.cpp
    mainwindow_menus.cpp

    etc.

    So I download latest (5.0.1) qt and open texteditor example and split into multiple c++ files.

    when I start to compile, I got lots of errors like,

    error: invalid use of incomplete type 'class QPlainTextEdit' and similar errors for other class'es

    Is there any way to do this or I'm trying to impossible.

    My best regards.

    Ps: If its possible, please give code example. Abstract information was too much for me. My php background helps me to handle things and my understanding c++ still problematic due to strong typing of variables.


  • Moderators

    It would be easier if you said what exact errors are you having. For example the "use of incomplete type X" usually means missing #include <X> in this cpp file.

    If you need to split cpps it's most likely that you've hit the "one class to rule them all" anti-pattern, meaning you're stuffing to much things into the main class. Maybe try to split them into logically separate classes like MainWindow, UserInputPanel, ComputationResultsPanel etc. depending what the app actually does.

    But of course you can split cpps, there's nothing impossible about it. Quite opposite actually, it's a common thing.
    A simplest example would be:

    @
    //mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class QPushButton;
    class QLineEdit;

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    explicit MainWindow(QWidget parent = 0);
    ~MainWindow();
    void someFunc1(QPushButton
    pb);
    void someFunc2(QLineEdit* le);
    signals:
    void somesignal1();
    void somesignal2();
    public slots:
    void slot1();
    void slot2();
    private:
    Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H

    //mainwindow_basic.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

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

    //mainwindow_1.cpp
    #include "mainwindow.h"
    #include <QPushButton>
    void MainWindow::someFunc1(QPushButton *pb)
    {
    //do something with pb
    }
    void MainWindow::slot1()
    {
    //...
    }

    //mainwindow_2.cpp
    #include "mainwindow.h"
    #include <QLineEdit>
    void MainWindow::someFunc2(QLineEdit *le)
    {
    //do something with le
    }
    void MainWindow::slot2()
    {
    //...
    }
    @



  • Hello again, your solution was works. Thank you for support.

    Also,

    My oo was limited (even in php standarts). Some languages (C, C++, Java, Python, Perl) likes abstract documentation.

    However this approach limits my kind of people (uneducated, sefl tough, lack of english) to understand full aspects of entire language. Thats why PHP so successfull. Entire PHP documentation has full working examples. (of course being a dynamic language was great plus)

    Full blown oo or using patterns in full capactiy needs to full understanding of oo concepts. which beyond my current level of c++ programming.

    Once upon a time I saw a article in /. which examines John Carmac's doom 3 code. Man it was an art.

    Anyway thanks again. This was much more better than current sate.

    My best regards...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.