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. Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise
Forum Updated to NodeBB v4.3 + New Features

Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 8 Posters 1.3k Views 2 Watching
  • 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.
  • I icebergenergy

    I have backend part all ready, now I want to build GUI with QT.
    I DONT want to use a lot of QT library functions.
    I DO want to keep my current code IN CURRENT FORM as much as possible and output results to QT forms and labels and execute calculations from the buttons.
    Basically I need to output data from dat file to some dialog window.
    Imagine I have dat file with items:
    1 (int) 2 (string) 3(string) 4 (double)
    1 John Doe 100


    100 Donald Trump 777

    Now I just overloaded outputting the data using function readFromDat, described below.
    I want transfer my code to QT GUI.
    I dont want to learn QT a lot, QDebug, Qfile, QDir, i would like to avoid that.
    My point: calculation in C++
    In/Out GUI QT.

    Please advise

    void outputLine(std::ostream& output, const Iesnc& record) {
    output << std::left << std::setw(10) << record.getAccountNumber()
    << std::setw(16) << record.getLastName()
    << std::setw(11) << record.getFirstName()
    << std::setw(10) << std::setprecision(2) << std::right << std::fixed
    << std::showpoint << record.getBalance() << '\n';
    }

    void readFromDat(std::fstream& masterStream) {
    if (!masterStream) {
    std::cerr << "File not opened.\n";
    return;
    }

    std::cout << std::left << std::setw(10) << "\nAccount" << std::setw(16)
    	<< "Last Name" << std::setw(11) << "First Name" << std::left
    	<< std::setw(11) << std::right << "Balance\n";
    
    masterStream.seekg(0);
    
    for (Iesnc client; masterStream.read(reinterpret_cast<char*>(&client), sizeof(Iesnc)); )
    	if (client.getAccountNumber() != 0)
    		outputLine(std::cout, client);
    
    masterStream.clear();
    

    }

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

    @icebergenergy said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

    I dont want to learn QT a lot

    You will have to learn at least some parts of Qt, how else would you use it?

    "Basically I need to output data from dat file to some dialog window" - you should say how you want to show your data. In a table?

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

    I 1 Reply Last reply
    0
    • D Offline
      D Offline
      DerReisende
      wrote on last edited by
      #3

      @icebergenergy You don‘t need to use all of Qt if not wanted. But then you may have to restructure your code for reuse.

      Based on your example, why doesn‘t readFromDat e.g. return a std::vector<DataObject> for the parsed classes? In there you could perform the account filtering or defer it to a later step. Then you would need to write two functions:

      • one for printing the data via std::cout
      • another one which e.g. creates a Qt table model for presentation in a Qt TableView for the UI part.

      In one of my applications I parse a several hundred MB csv file with an external parser library (c++20), do some BOOST magic and calculations on it and get a vector of objects at the end. This is the base for a tree view model.
      99% of the code is std c++ until you have to interact with Qt obviously. But perfectly doable.

      Although I do admit that Qt sometimes would simplify my life if I would use it more often in the std c++ code…

      1 Reply Last reply
      0
      • jsulmJ jsulm

        @icebergenergy said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

        I dont want to learn QT a lot

        You will have to learn at least some parts of Qt, how else would you use it?

        "Basically I need to output data from dat file to some dialog window" - you should say how you want to show your data. In a table?

        I Offline
        I Offline
        icebergenergy
        wrote on last edited by
        #4

        @jsulm table is fine, could be any other output methods you could recommend

        @DerReisende said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

        @icebergenergy You don‘t need to use all of Qt if not wanted. But then you may have to restructure your code for reuse.

        Based on your example, why doesn‘t readFromDat e.g. return a std::vector<DataObject> for the parsed classes? In there you could perform the account filtering or defer it to a later step. Then you would need to write two functions:

        • one for printing the data via std::cout
        • another one which e.g. creates a Qt table model for presentation in a Qt TableView for the UI part.

        In one of my applications I parse a several hundred MB csv file with an external parser library (c++20), do some BOOST magic and calculations on it and get a vector of objects at the end. This is the base for a tree view model.
        99% of the code is std c++ until you have to interact with Qt obviously. But perfectly doable.

        Although I do admit that Qt sometimes would simplify my life if I would use it more often in the std c++ code…

        first of all, im stuck with fstream and Qwidget. Is there any way to cooperate like this or I have to use Q stream operators only?
        #include "mainwindow.h"
        #include <QApplication >
        #include <QLabel>
        #include <iostream>
        #include <fstream>
        #include <cstdlib>
        #include <string>
        #include "iesnc.h"
        #include <iomanip>

        fstream masterStream ( "C:/Users/iceberg/Desktop/kocgun/march/iesnc/iesnc/master.dat", ios::in | ios::out | ios::binary );
        fstream saveLoadDatabase ( "C:/Users/iceberg/Desktop/kocgun/march/iesnc/iesnc/databaseCopy.dat", ios::in | ios::out | ios::binary );

        int main(int argc, char *argv[])
        {

        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
        

        }

        Pl45m4P JonBJ 2 Replies Last reply
        0
        • I icebergenergy

          @jsulm table is fine, could be any other output methods you could recommend

          @DerReisende said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

          @icebergenergy You don‘t need to use all of Qt if not wanted. But then you may have to restructure your code for reuse.

          Based on your example, why doesn‘t readFromDat e.g. return a std::vector<DataObject> for the parsed classes? In there you could perform the account filtering or defer it to a later step. Then you would need to write two functions:

          • one for printing the data via std::cout
          • another one which e.g. creates a Qt table model for presentation in a Qt TableView for the UI part.

          In one of my applications I parse a several hundred MB csv file with an external parser library (c++20), do some BOOST magic and calculations on it and get a vector of objects at the end. This is the base for a tree view model.
          99% of the code is std c++ until you have to interact with Qt obviously. But perfectly doable.

          Although I do admit that Qt sometimes would simplify my life if I would use it more often in the std c++ code…

          first of all, im stuck with fstream and Qwidget. Is there any way to cooperate like this or I have to use Q stream operators only?
          #include "mainwindow.h"
          #include <QApplication >
          #include <QLabel>
          #include <iostream>
          #include <fstream>
          #include <cstdlib>
          #include <string>
          #include "iesnc.h"
          #include <iomanip>

          fstream masterStream ( "C:/Users/iceberg/Desktop/kocgun/march/iesnc/iesnc/master.dat", ios::in | ios::out | ios::binary );
          fstream saveLoadDatabase ( "C:/Users/iceberg/Desktop/kocgun/march/iesnc/iesnc/databaseCopy.dat", ios::in | ios::out | ios::binary );

          int main(int argc, char *argv[])
          {

          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
          

          }

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #5

          @icebergenergy said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

          first of all, im stuck with fstream and Qwidget. Is there any way to cooperate like this or I have to use Q stream operators only?

          What do you want to do with your data? Where to show or use it? It's always better to use the Q... stuff when interacting with your Qt GUI, since it will just work better. But doesn't mean plain C++ or some boost stuff won't work either.

          For example QFile or QDataStream... It's not that complicated.

          • https://doc.qt.io/qt-5/qdatastream.html#details

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          2
          • I icebergenergy

            @jsulm table is fine, could be any other output methods you could recommend

            @DerReisende said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

            @icebergenergy You don‘t need to use all of Qt if not wanted. But then you may have to restructure your code for reuse.

            Based on your example, why doesn‘t readFromDat e.g. return a std::vector<DataObject> for the parsed classes? In there you could perform the account filtering or defer it to a later step. Then you would need to write two functions:

            • one for printing the data via std::cout
            • another one which e.g. creates a Qt table model for presentation in a Qt TableView for the UI part.

            In one of my applications I parse a several hundred MB csv file with an external parser library (c++20), do some BOOST magic and calculations on it and get a vector of objects at the end. This is the base for a tree view model.
            99% of the code is std c++ until you have to interact with Qt obviously. But perfectly doable.

            Although I do admit that Qt sometimes would simplify my life if I would use it more often in the std c++ code…

            first of all, im stuck with fstream and Qwidget. Is there any way to cooperate like this or I have to use Q stream operators only?
            #include "mainwindow.h"
            #include <QApplication >
            #include <QLabel>
            #include <iostream>
            #include <fstream>
            #include <cstdlib>
            #include <string>
            #include "iesnc.h"
            #include <iomanip>

            fstream masterStream ( "C:/Users/iceberg/Desktop/kocgun/march/iesnc/iesnc/master.dat", ios::in | ios::out | ios::binary );
            fstream saveLoadDatabase ( "C:/Users/iceberg/Desktop/kocgun/march/iesnc/iesnc/databaseCopy.dat", ios::in | ios::out | ios::binary );

            int main(int argc, char *argv[])
            {

            QApplication a(argc, argv);
            MainWindow w;
            w.show();
            return a.exec();
            

            }

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #6

            @icebergenergy said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

            first of all, im stuck with fstream and Qwidget

            In addition to @Pl45m4. What do you want to do with fstream that has anything to do with QWidget? Your code just shows two fstreams initialised for output to the file paths, no more than that, what is "stuck"?

            1 Reply Last reply
            0
            • I Offline
              I Offline
              icebergenergy
              wrote on last edited by
              #7

              Once again, I want to keep C++ syntax, and use QT for executing C++ functions.
              What is good option to output binary files with QT?
              Qlabel?

              Christian EhrlicherC 1 Reply Last reply
              0
              • I icebergenergy

                Once again, I want to keep C++ syntax, and use QT for executing C++ functions.
                What is good option to output binary files with QT?
                Qlabel?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @icebergenergy said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

                I want to keep C++ syntax, and use QT for executing C++ functions.

                You're aware that Qt is a C++ library? So your statement somehow doesn't make sense.
                @JonB asked about your real problem - what's your task you're stuck in? What did you try? What do you want to achieve?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • I Offline
                  I Offline
                  icebergenergy
                  wrote on last edited by
                  #9

                  I need to output DAT file lines to QT Window, all calculations in DAT files, output in GUI.

                  [1] Find Interface to output
                  [2] On button click - read from DAT file function
                  [3] Output Line By Line to GUI interface [1]

                  Read with C++ syntax
                  Output to GUI with Q Syntax

                  I have system working without GUI, I need GUI, need be to implemented step by step.
                  Where Q syntax is MUST I will go deep dive and transit there, if not I will keep C++ syntax

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    And what did you try so far? Where are your problems? There are a lot of examples on how to build a simple Qt GUI application out there.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1
                    • I Offline
                      I Offline
                      icebergenergy
                      wrote on last edited by
                      #11

                      I've tried QLabel, I think it will be fine.
                      But I need proper sample.
                      So I'm here for your kind advise.
                      Need to output 4 columns from DAT file
                      BR

                      Dima

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • I icebergenergy

                        I've tried QLabel, I think it will be fine.
                        But I need proper sample.
                        So I'm here for your kind advise.
                        Need to output 4 columns from DAT file
                        BR

                        Dima

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @icebergenergy said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

                        But I need proper sample.

                        I gave you a link to a lot of samples. Start with a basic one to get some ideas on how it works, then try to add your functionality.
                        Noone will write the code for you here.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          icebergenergy
                          wrote on last edited by
                          #13

                          @Christian-Ehrlicher said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

                          And what did you try so far? Where are your problems? There are a lot of examples on how to build a simple Qt GUI application out there.

                          Bro, I've researched your link, and found interesting QTableView as a future library.
                          What do you think of QTableView in regard to my case?

                          JonBJ 1 Reply Last reply
                          0
                          • I icebergenergy

                            @Christian-Ehrlicher said in Need to build QT GUI using my C++ code. I want to be more C++ and less QT. Please advise:

                            And what did you try so far? Where are your problems? There are a lot of examples on how to build a simple Qt GUI application out there.

                            Bro, I've researched your link, and found interesting QTableView as a future library.
                            What do you think of QTableView in regard to my case?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #14

                            @icebergenergy
                            It's not clear (to me at least) what is in your "DAT file", and what you want to output/appear in a GUI.

                            If you have some kind of (text) file with lines and multiple "fields" per line, separated somehow --- ahh, I see, 100 Donald Trump 777 --- and you would like to display it as rows (for the lines) and columns (for the fields), then a QGridView or QTableView might be good choices.

                            These two are both "visual" (hence the word "view" at the end). You have to provide the data you want them to show in a suitable form. In the case of a QTableView that needs a model for its data. The model holds rows & columns of data, the view is attached to the model and displays it.

                            Have a look at QTableWidget. That is a QTableView (for viewing) plus an internal model (for the data) combined into one widget. Read the lines from your file, split into appropriate fields, put that data into the QTableWidget as you go and it will display as rows & columns.

                            1 Reply Last reply
                            5
                            • S Offline
                              S Offline
                              SimonSchroeder
                              wrote on last edited by
                              #15

                              I agree with @JonB. The decision if you want to use QTableWidget or QTableView mostly depends on how many lines you expect to display. The easy approach is to use a QTableWidget with setItem(). setItem needs a QTableWidgetItem and its constructor takes a string (QString, obviously). So you can do a loop over all your data, construct QTableWidgetItems and place them in the table widget.

                              If you don't do that too often (i.e. data does not change) and if you don't have to many items, this approach will work just fine. Otherwise you'll need a QTableView (QTableWidget actually derives from it) together with a model. For the model you have to derive from the proper class and overwrite the method that will yield the data for a column/row pair. How you retrieve the data is again up to you (just use plain C++ without Qt for the retrieval).

                              JonBJ 1 Reply Last reply
                              1
                              • S SimonSchroeder

                                I agree with @JonB. The decision if you want to use QTableWidget or QTableView mostly depends on how many lines you expect to display. The easy approach is to use a QTableWidget with setItem(). setItem needs a QTableWidgetItem and its constructor takes a string (QString, obviously). So you can do a loop over all your data, construct QTableWidgetItems and place them in the table widget.

                                If you don't do that too often (i.e. data does not change) and if you don't have to many items, this approach will work just fine. Otherwise you'll need a QTableView (QTableWidget actually derives from it) together with a model. For the model you have to derive from the proper class and overwrite the method that will yield the data for a column/row pair. How you retrieve the data is again up to you (just use plain C++ without Qt for the retrieval).

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #16

                                @SimonSchroeder
                                Agreed. I suggested QTableWidget rather than QTreeView for this OP as I think he is looking for a simple solution and this avoids him having to write a model himself for a QTreeView.

                                1 Reply Last reply
                                1
                                • D Offline
                                  D Offline
                                  Dan203
                                  wrote on last edited by
                                  #17

                                  Just use the designer to create .ui files for the dialogs you need. There is a simple API to load these files as a class and every control has simple method for setting/reading it's value. Requires very little code.

                                  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