Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How to run a C++ file from Qt?
QtWS25 Last Chance

How to run a C++ file from Qt?

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
21 Posts 4 Posters 5.5k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #6

    Then the next question: why not integrate that C++ file functionality in your application rather than having it outside ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mayathebee
      wrote on last edited by
      #7

      I'll try to do that and I'll come back later if I don't figure out the solution.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mayathebee
        wrote on last edited by
        #8

        I'm stuck. This is what is written in Application Output after running my project:
        Starting C:\build-project-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\project.exe...
        Greska pri ucitavanju datoteke1.C:\build-project-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\project.exe exited with code 0.
        'Greska pri ucitavanju datoteke' translated to English is something like 'error while loading the file'.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #9

          Which file ?
          What is the related code ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mayathebee
            wrote on last edited by
            #10

            I guess the first file in my Source dirctory because of the 'datoteke1' meaning file 1. I don't understand why it's not separated. Anyway, that file is the file I added to Source directory. I can compile and run it outside of Qt, and I can build it in Qt. It has 731 lines so I'm not sure if I should post it in here.

            jsulmJ 1 Reply Last reply
            0
            • M mayathebee

              I guess the first file in my Source dirctory because of the 'datoteke1' meaning file 1. I don't understand why it's not separated. Anyway, that file is the file I added to Source directory. I can compile and run it outside of Qt, and I can build it in Qt. It has 731 lines so I'm not sure if I should post it in here.

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

              @mayathebee Can you show your pro file?

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mayathebee
                wrote on last edited by mayathebee
                #12
                
                QT       += core gui
                
                greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                
                TARGET = najnoviji
                TEMPLATE = app
                
                DEFINES += QT_DEPRECATED_WARNINGS
                
                SOURCES += main.cpp\
                        mainwindow.cpp \
                    kod.cpp
                
                HEADERS  += mainwindow.h
                
                FORMS    += mainwindow.ui
                
                RESOURCES += \
                    resources.qrc
                

                Maybe the first file in Source is main.cpp, not kod.cpp.

                jsulmJ 1 Reply Last reply
                0
                • M mayathebee
                  
                  QT       += core gui
                  
                  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                  
                  TARGET = najnoviji
                  TEMPLATE = app
                  
                  DEFINES += QT_DEPRECATED_WARNINGS
                  
                  SOURCES += main.cpp\
                          mainwindow.cpp \
                      kod.cpp
                  
                  HEADERS  += mainwindow.h
                  
                  FORMS    += mainwindow.ui
                  
                  RESOURCES += \
                      resources.qrc
                  

                  Maybe the first file in Source is main.cpp, not kod.cpp.

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

                  @mayathebee said in How to run a C++ file from Qt?:

                  kod.cpp

                  is this the file you added?
                  So, your app is building but doesn't start?

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

                  M 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @mayathebee said in How to run a C++ file from Qt?:

                    kod.cpp

                    is this the file you added?
                    So, your app is building but doesn't start?

                    M Offline
                    M Offline
                    mayathebee
                    wrote on last edited by
                    #14

                    Yes. I think the app is building correctly because there are no errors. When I run it, the messagges that I wrote before show in Application Output.

                    jsulmJ 1 Reply Last reply
                    0
                    • M mayathebee

                      Yes. I think the app is building correctly because there are no errors. When I run it, the messagges that I wrote before show in Application Output.

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

                      @mayathebee Start your app in debug mode and post the stack trace here.
                      Also you could post here the content of main.cpp and mainwindow.* maybe we can see what you're doing wrongly.

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

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mayathebee
                        wrote on last edited by mayathebee
                        #16

                        Debugging starts
                        Greska pri ucitavanju datoteke1.Debugging has finished

                        main.cpp

                        #include "mainwindow.h"
                        #include <QApplication>
                        
                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                            MainWindow w;
                            w.show();
                        
                            return a.exec();
                        }
                        

                        mainwindow.cpp

                        #include "mainwindow.h"
                        #include "ui_mainwindow.h"
                        #include <QDir>
                        #include <QDebug>
                        #include <QMessageBox>
                        #include <QProcess>
                        #include <QString>
                        #include <QStringList>
                        #include <QDesktopServices>
                        #include <QUrl>
                        
                        MainWindow::MainWindow(QWidget *parent) :
                            QMainWindow(parent),
                            ui(new Ui::MainWindow)
                        {
                            ui->setupUi(this);
                        
                            ui->comboBox->addItem("Nije odabran program.");
                            QDir dir(":/");
                            foreach (QFileInfo var, dir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries))
                                if(var.suffix()=="cpp")
                                    ui->comboBox->addItem(var.fileName());
                        
                            ui->comboBox_2->addItem("Nije odabrana datoteka.");
                            foreach (QFileInfo var, dir.entryList(QDir::NoDotAndDotDot|QDir::AllEntries))
                                if(var.suffix()=="txt" && var.baseName()!="podaci" && var.baseName()!="izlaz")
                                    ui->comboBox_2->addItem(var.fileName());
                        }
                        
                        MainWindow::~MainWindow()
                        {
                            delete ui;
                        }
                        
                        void MainWindow::on_pushButton_clicked()
                        {
                            QFile file("C:/najnoviji/podaci.txt"); //treba promijeniti ove puteve
                            if (!file.open(QFile::WriteOnly | QFile::Text))
                                QMessageBox::warning(this, "title", "File not open.");
                            else{
                                QTextStream out(&file);
                                QString text = ui->comboBox->currentText()+"\n"+ui->comboBox_2->currentText();
                                out << text;
                                file.flush();
                                file.close();
                            }
                        
                            QString fileName = ui->comboBox->currentText();
                            fileName=fileName.section(".",0,0);
                            QDesktopServices::openUrl(QUrl("file:///C:/najnoviji/"+fileName+".exe",QUrl::TolerantMode));
                        
                            /*
                            QString program = "C:/bla/Program/kod1.cpp";
                            QStringList arguments;
                            arguments << ui->comboBox_2->currentText();
                            QProcess *myProcess = new QProcess(this);
                            myProcess->start(program, arguments);
                            //normalan izlaz
                            //exitcode prazno
                            */
                        
                            QFile file2("C:/najnoviji/izlaz.txt");
                            if (!file2.open(QFile::ReadOnly | QFile::Text))
                                QMessageBox::warning(this, "title", "File not open.");
                            else{
                                ui->lineEdit->setText("");
                                QTextStream in(&file2);
                                QString text = in.readLine();
                                ui->lineEdit->setText(text);
                                // ne zaboravi isprazniti datoteku izlaz.txt
                                file2.close();
                            }
                        }
                        
                        
                        JonBJ jsulmJ 2 Replies Last reply
                        0
                        • M mayathebee

                          Debugging starts
                          Greska pri ucitavanju datoteke1.Debugging has finished

                          main.cpp

                          #include "mainwindow.h"
                          #include <QApplication>
                          
                          int main(int argc, char *argv[])
                          {
                              QApplication a(argc, argv);
                              MainWindow w;
                              w.show();
                          
                              return a.exec();
                          }
                          

                          mainwindow.cpp

                          #include "mainwindow.h"
                          #include "ui_mainwindow.h"
                          #include <QDir>
                          #include <QDebug>
                          #include <QMessageBox>
                          #include <QProcess>
                          #include <QString>
                          #include <QStringList>
                          #include <QDesktopServices>
                          #include <QUrl>
                          
                          MainWindow::MainWindow(QWidget *parent) :
                              QMainWindow(parent),
                              ui(new Ui::MainWindow)
                          {
                              ui->setupUi(this);
                          
                              ui->comboBox->addItem("Nije odabran program.");
                              QDir dir(":/");
                              foreach (QFileInfo var, dir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries))
                                  if(var.suffix()=="cpp")
                                      ui->comboBox->addItem(var.fileName());
                          
                              ui->comboBox_2->addItem("Nije odabrana datoteka.");
                              foreach (QFileInfo var, dir.entryList(QDir::NoDotAndDotDot|QDir::AllEntries))
                                  if(var.suffix()=="txt" && var.baseName()!="podaci" && var.baseName()!="izlaz")
                                      ui->comboBox_2->addItem(var.fileName());
                          }
                          
                          MainWindow::~MainWindow()
                          {
                              delete ui;
                          }
                          
                          void MainWindow::on_pushButton_clicked()
                          {
                              QFile file("C:/najnoviji/podaci.txt"); //treba promijeniti ove puteve
                              if (!file.open(QFile::WriteOnly | QFile::Text))
                                  QMessageBox::warning(this, "title", "File not open.");
                              else{
                                  QTextStream out(&file);
                                  QString text = ui->comboBox->currentText()+"\n"+ui->comboBox_2->currentText();
                                  out << text;
                                  file.flush();
                                  file.close();
                              }
                          
                              QString fileName = ui->comboBox->currentText();
                              fileName=fileName.section(".",0,0);
                              QDesktopServices::openUrl(QUrl("file:///C:/najnoviji/"+fileName+".exe",QUrl::TolerantMode));
                          
                              /*
                              QString program = "C:/bla/Program/kod1.cpp";
                              QStringList arguments;
                              arguments << ui->comboBox_2->currentText();
                              QProcess *myProcess = new QProcess(this);
                              myProcess->start(program, arguments);
                              //normalan izlaz
                              //exitcode prazno
                              */
                          
                              QFile file2("C:/najnoviji/izlaz.txt");
                              if (!file2.open(QFile::ReadOnly | QFile::Text))
                                  QMessageBox::warning(this, "title", "File not open.");
                              else{
                                  ui->lineEdit->setText("");
                                  QTextStream in(&file2);
                                  QString text = in.readLine();
                                  ui->lineEdit->setText(text);
                                  // ne zaboravi isprazniti datoteku izlaz.txt
                                  file2.close();
                              }
                          }
                          
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #17

                          @mayathebee
                          So far as I can see, this has nothing to do with what the program tries to do in itself, it's to do with the building & running of the program.

                          Try an "empty" program. Does it do the same, regardless of content? Are your compilation flags the same in Qt as outside? Are your run-time libraries paths different? Can you place a debug breakpoint on the very first line? Is the 32-bitness to do with the issue?

                          1 Reply Last reply
                          0
                          • M mayathebee

                            Debugging starts
                            Greska pri ucitavanju datoteke1.Debugging has finished

                            main.cpp

                            #include "mainwindow.h"
                            #include <QApplication>
                            
                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                                MainWindow w;
                                w.show();
                            
                                return a.exec();
                            }
                            

                            mainwindow.cpp

                            #include "mainwindow.h"
                            #include "ui_mainwindow.h"
                            #include <QDir>
                            #include <QDebug>
                            #include <QMessageBox>
                            #include <QProcess>
                            #include <QString>
                            #include <QStringList>
                            #include <QDesktopServices>
                            #include <QUrl>
                            
                            MainWindow::MainWindow(QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::MainWindow)
                            {
                                ui->setupUi(this);
                            
                                ui->comboBox->addItem("Nije odabran program.");
                                QDir dir(":/");
                                foreach (QFileInfo var, dir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries))
                                    if(var.suffix()=="cpp")
                                        ui->comboBox->addItem(var.fileName());
                            
                                ui->comboBox_2->addItem("Nije odabrana datoteka.");
                                foreach (QFileInfo var, dir.entryList(QDir::NoDotAndDotDot|QDir::AllEntries))
                                    if(var.suffix()=="txt" && var.baseName()!="podaci" && var.baseName()!="izlaz")
                                        ui->comboBox_2->addItem(var.fileName());
                            }
                            
                            MainWindow::~MainWindow()
                            {
                                delete ui;
                            }
                            
                            void MainWindow::on_pushButton_clicked()
                            {
                                QFile file("C:/najnoviji/podaci.txt"); //treba promijeniti ove puteve
                                if (!file.open(QFile::WriteOnly | QFile::Text))
                                    QMessageBox::warning(this, "title", "File not open.");
                                else{
                                    QTextStream out(&file);
                                    QString text = ui->comboBox->currentText()+"\n"+ui->comboBox_2->currentText();
                                    out << text;
                                    file.flush();
                                    file.close();
                                }
                            
                                QString fileName = ui->comboBox->currentText();
                                fileName=fileName.section(".",0,0);
                                QDesktopServices::openUrl(QUrl("file:///C:/najnoviji/"+fileName+".exe",QUrl::TolerantMode));
                            
                                /*
                                QString program = "C:/bla/Program/kod1.cpp";
                                QStringList arguments;
                                arguments << ui->comboBox_2->currentText();
                                QProcess *myProcess = new QProcess(this);
                                myProcess->start(program, arguments);
                                //normalan izlaz
                                //exitcode prazno
                                */
                            
                                QFile file2("C:/najnoviji/izlaz.txt");
                                if (!file2.open(QFile::ReadOnly | QFile::Text))
                                    QMessageBox::warning(this, "title", "File not open.");
                                else{
                                    ui->lineEdit->setText("");
                                    QTextStream in(&file2);
                                    QString text = in.readLine();
                                    ui->lineEdit->setText(text);
                                    // ne zaboravi isprazniti datoteku izlaz.txt
                                    file2.close();
                                }
                            }
                            
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #18

                            @mayathebee You should at least try to debug step by step. Put a break point at

                            ui->setupUi(this);
                            

                            and then execute step by step to see where it crashes.

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

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mayathebee
                              wrote on last edited by
                              #19

                              Sorry, I don't know what was wrong with me, the 'Greska pri ucitavanju datoteke' was from file kod.cpp and I think it was happening because I didn't give the absoulte path of files to functions open. Here is the code of kod.cpp (you can search for 'Greska' and you'll see where I would write it).

                              #include <stdio.h>
                              #include <stdlib.h>
                              #include <iostream>
                              #include <sstream>
                              #include <fstream>
                              #include <string.h>
                              #include <time.h>
                              #include <ctime>
                              #include <vector>
                              #include <string.h>
                              
                              using namespace std;
                              
                              string ime_datoteke;
                              int **bridovi;
                              int *klika01;
                              int *koliko_puta_nesusjed;
                              int *pozicija;
                              int *tabu_lista;
                              int broj_vrhova=0;
                              int dubina;
                              int *broj_nesusjeda;
                              int **matrica;
                              int *klika;
                              int len;
                              int length;
                              int *kandidati_za_add;
                              int *kandidati_za_swap;
                              int *tezine;
                              int *nije_susjed;
                              int duljina1;
                              int duljina2;
                              int *niz1;
                              int *niz2;
                              int iteracija;
                              int tabu_tenur = 7;
                              int tezina_klike;
                              int najbolja_tezina;
                              
                              
                              int *Tbest;
                              int Titer;
                              double start, kraj, avg_time;
                              int bestLen = 0;
                              int len_W;
                              int Iteration[ 100 ];
                              double time_used[ 100 ];
                              int len_used[ 100 ];
                              int W_used[ 100 ];
                              char outfilename[30];
                              int len_time;
                              int notImproved;
                              
                              void inicijalizacija()
                              {
                                  ifstream FIC;
                              
                                  string c="C:/najnoviji/";
                                  c+=ime_datoteke;
                                  const char * c2 = c.c_str();
                              
                                  FIC.open(c2);
                                  if ( FIC.fail() )
                                  {
                                      cout << "Greska pri ucitavanju datoteke.2"<< endl;
                                      getchar();
                                      exit(0);
                                  }
                                  char citaj[100];
                                  FIC >> citaj;
                                  if ( FIC.eof() )
                                  {
                                      cout << "Datoteka je prazna."<< endl;
                                      exit(0);
                                  }
                                  int broj_bridova=-1;
                                  int x, y;
                                  if ( strcmp(citaj, "p" )==0 )
                                  {
                                      FIC >> broj_vrhova >> broj_bridova;
                                      //cout << "Broj vrhova = " << broj_vrhova << endl;
                                      //cout << "Broj bridova = " << broj_bridova << endl;
                              
                                      klika01 = new int [broj_vrhova];
                                      koliko_puta_nesusjed  = new int [broj_vrhova];
                                      pozicija= new int [broj_vrhova];
                                      tabu_lista = new int [broj_vrhova];
                                      broj_nesusjeda= new int[broj_vrhova];
                                      kandidati_za_add = new int[broj_vrhova];
                                      kandidati_za_swap = new int[broj_vrhova];
                                      niz2= new int[broj_vrhova];
                                      tezine = new int[broj_vrhova];
                                      nije_susjed = new int[broj_vrhova];
                                      niz1= new int[broj_vrhova];
                                      matrica = new int*[broj_vrhova];
                                      klika = new int [2000];
                                      Tbest = new int[broj_vrhova];
                              
                                      bridovi = new int*[ broj_vrhova ];
                              
                                      for (int x = 0 ; x < broj_vrhova ; x++ )
                                      {
                                          bridovi[x] = new int[broj_vrhova];
                                          matrica[x] = new int[broj_vrhova];
                                      }
                                      for ( int x=0; x<broj_vrhova; x++ )
                                          for ( int y=0; y<broj_vrhova; y++ )
                                              bridovi[x][y] = 1;
                                  }
                                  FIC >> x;
                                  while ( ! FIC.eof() )
                                  {
                                      FIC >> y;
                                      x--; y--;
                                      bridovi[x][y]=bridovi[y][x]=0;
                                      FIC >> x;
                                  }
                              
                                  //cout << "Gustoca = " << 2*(float) broj_bridova/(broj_vrhova*(broj_vrhova-1)) << endl;
                              
                                   for( int x=0 ; x<broj_vrhova; x++ )
                                       bridovi[x][x] = 0;
                              
                                   for( int x=0 ; x<broj_vrhova; x++ ){
                                       broj_nesusjeda[x] = 0;
                                       for( int y=0; y<broj_vrhova; y++ )
                                          if( bridovi[x][y] == 1 ){
                                              matrica[x][broj_nesusjeda[x]] = y;
                                              broj_nesusjeda[x]++;
                                          }
                                   }
                              
                                   for( int x = 0; x < broj_vrhova; x++ ){
                                      tezine[ x ] = (x+1)%200 + 1;
                                      nije_susjed[ x ] = 0;
                                   }
                              
                                   FIC.close();
                              }
                              
                              int randomInt( int n ) {
                                  return rand() % n;
                              }
                              
                              void ocisti()
                              {
                                  int i;
                                  memset( klika01, 0, length );
                                  memset(  koliko_puta_nesusjed, 0, length );
                                  memset(pozicija, 0, length );
                                  memset( tabu_lista, 0, length );
                                  for( i = 0; i < broj_vrhova; i++ )
                                  {
                                     kandidati_za_add[ i ] = i;
                                     pozicija[ i ] = i;
                                  }
                                  duljina1 = broj_vrhova;
                                  duljina2 = 0;
                                  len = 0;
                                  tezina_klike = 0;
                                  najbolja_tezina = 0;
                              }
                              
                              int odaberi_random_vrh( )
                              {
                                  if( duljina1 )
                                     return randomInt( duljina1 );
                                  else
                                      return -1;
                              }
                              
                              int odaberi_vrh_za_add( ) // trazimo indeks vrha za Add
                              {
                                  int k, l1, l2, w1, w2;
                                  l1 = 0;
                                  l2 = 0;
                                  w1 = 0;
                                  w2 = 0;
                              
                                  for(int i = 0; i < duljina1; i++ )
                                  {
                                     k = kandidati_za_add[ i ];
                                     if( tabu_lista[ k ] <= iteracija )
                                     {
                                         if( tezine[ k ] > w1 )
                                         {
                                            l1 = 0;
                                            w1 = tezine[ k ];
                                            niz1[ l1++ ] = i;
                                         }
                                         else if ( tezine[ k ] == w1 )
                                            niz1[ l1++ ] = i;
                                     }
                                     else
                                     {
                                         if( tezine[ k ] > w2 )
                                         {
                                            l2 = 0;
                                            w2 = tezine[ k ];
                                            niz2[ l2++ ] = i;
                                         }
                                         else if ( tezine[ k ] == w2 )
                                            niz2[ l2++ ] = i;
                                     }
                                  }
                              
                                  if( (l2 > 0) && ( w2 > w1 ) && ((w2+tezina_klike)>najbolja_tezina) ) // kriterij aspiracije
                                  {
                                      k = randomInt( l2 );
                                      k = niz2[ k ];
                                      return k;
                                  }
                              
                                  else if( l1 > 0 )
                                  {
                                      k = randomInt( l1 );
                                      k = niz1[ k ];
                                      return k;
                                  }
                              
                                  else
                                      return -1;
                              }
                              
                              int Add(int x) // u kliku dodajemo vrh s indeksom x u nizu kandidati_za_add
                              {
                                  int k1, m, n, n1;
                              
                                  m = kandidati_za_add[ x ];
                                  klika[ len++ ] = m;
                                  klika01[ m ] = 1;
                                  tezina_klike += tezine[ m ];
                              
                                  duljina1--;
                                  n1 = kandidati_za_add[ duljina1 ];
                                  k1 = pozicija[ m ];
                                  kandidati_za_add[ k1 ] = n1;
                                  pozicija[ n1 ] = k1;
                              
                                  for(int i = 0; i < broj_nesusjeda[ m ]; i++ )
                                  {
                                     n = matrica[ m ][ i ];
                                     koliko_puta_nesusjed[ n ]++; // koliko_puta_nesusjed - indeksi su vrhovi, a elementi oznacavaju koliko puta se vrh n pojavio kao NEsusjed elemenata iz klike
                                     if( koliko_puta_nesusjed[ n ] == 1 ) // prvi put se pojavio kao nesusjed
                                     {
                                         // izbacujemo vrh n iz kandidati_za_add jer nije susjedan sa svima iz klike (pa ga ne mozemo dodati u kliku)
                                         k1 = pozicija[ n ];
                                         duljina1--;
                                         n1 = kandidati_za_add[ duljina1 ];
                                         kandidati_za_add[ k1 ] = n1;
                                         pozicija[ n1 ] = k1;
                              
                                         // u kandidati_za_swap spremamo sve vrhove koji nisu susjedi tocno s jednim vrhom iz klike; nad njima se moze izvrsiti operacija SWAP (mijenjaju se s nekim vrhom iz klike); duljina je duljina2
                                         kandidati_za_swap[ duljina2 ] = n;
                                         pozicija[ n ] = duljina2;
                                         duljina2++;
                                         nije_susjed[ n ] = m; // nije_susjed - indeksi su vrhovi iz kandidati_za_swap, elementi su jedini vrhovi iz klike s kojima vrhovi s odgovarajucim ineksom nisu povezani (m je jedini vrh iz klike s kojim n nije povezan)
                                     }
                              
                                     else if( koliko_puta_nesusjed[ n ] == 2 ) // vrh n se je dvaput pojavio kao neciji nesusjed (nesusjed neka dva vrha iz klike)
                                     {
                                         // nad vrhom n se ne moze izvesti operacija SWAP jer bi inace u kliki bio jos jedan element s kojim taj vrh ne bi bio povezan
                                         //pa ta operacija ne bi bila valjana (niz klika vise ne bi bio klika); zato brisemo n iz kandidati_za_swap
                                         duljina2--;
                                         n1 = kandidati_za_swap[ duljina2 ];
                                         k1 = pozicija[ n ];
                                         kandidati_za_swap[ k1 ] = n1;
                                         pozicija[ n1 ] = k1;
                                     }
                                  }
                              
                                  if( tezina_klike > najbolja_tezina )
                                   {
                                       notImproved=0;
                                      najbolja_tezina = tezina_klike;
                                      bestLen = len;
                                      /*for( i = 0; i < broj_vrhova; i++ )
                                          Tbest[ i ] = klika01[ i ]; */
                                   }
                                  notImproved++;
                                  return 1;
                              }
                              
                              int odaberi_vrh_za_swap( ) // vraca poziciju vrha u polju kandidati_za_swap kojeg trebamo odabrati ako zelimo izvrsiti operaciju SWAP
                              {
                                   int k, l1, l2, w, w1, w2, m, n;
                                   l1 = 0;
                                   l2 = 0;
                                   w1 = -1000000;
                                   w2 = -1000000;
                                   for(int i = 0; i < duljina2; i++ )
                                   {
                                       m = kandidati_za_swap[ i ];
                                       n = nije_susjed[ m ];
                              
                                       if( (klika01[ n ] != 1) || (bridovi[ m ][ n ] != 1) ){ // ovo radimo ako u nije_susjed pise nesto krivo, tj. n nije u kliki ili su n i m povezani
                                           for(int j = 0; j < len; j++ )
                                           {
                                              k = klika[ j ];
                                              if( bridovi[ m ][ k ] == 1 )
                                                break;
                                           }
                                           nije_susjed[ m ] = k;
                                       }
                                   }
                              
                                   // analogno kao u odaberi_vrh_za_add()...
                                   for(int i = 0; i < duljina2; i++ )
                                   {
                                       m = kandidati_za_swap[ i ];
                                       n = nije_susjed[ m ];
                                       w = tezine[ m ] - tezine[ n ]; //... osim sto ovako racunamo tezinu operacije SWAP jer zelimo dodati m i izbaciti n
                                       if( tabu_lista[ m ] <= iteracija )
                                       {
                                           if( w > w1 )
                                           {
                                              l1 = 0;
                                              w1 = w;
                                              niz1[ l1++ ] = i;
                                           }
                                           else if ( w == w1 )
                                           {
                                              niz1[ l1++ ] = i;
                                           }
                                       }
                                       else
                                       {
                                           if( w > w2 )
                                           {
                                              l2 = 0;
                                              w2 = w;
                                              niz2[ l2++ ] = i;
                                           }
                                           else if ( w == w2 )
                                           {
                                              niz2[ l2++ ] = i;
                                           }
                                       }
                                   }
                              
                                   if( (l2 > 0) && ( w2 > w1 ) && ((w2+tezina_klike)>najbolja_tezina) )
                                   {
                                       k = randomInt( l2 );
                                       k = niz2[ k ];
                                       return k;
                                   }
                                   else if( l1 > 0 )
                                   {
                                       k = randomInt( l1 );
                                       k = niz1[ k ];
                                       return k;
                                   }
                                   else
                                   {
                                       return -1;
                                   }
                              }
                              
                              int Swap( int x )
                              {
                                   int i, k1, m, m1, n, n1, ti;
                              
                                   m = kandidati_za_swap[ x ];
                                   for(ti = 0; ti < len; ti++)
                                   {
                                       m1 = klika[ ti ];
                                       if( bridovi[ m1 ][ m ] == 1 ) // prvi iz klika s kojim m nije povezan (ne znam zas nece m1=nije_susjed[m])
                                          break;
                                   }
                                   if(nije_susjed[m]!=m1) cout<<"razl"<<endl;
                              
                                   tezina_klike = tezina_klike + tezine[ m ] - tezine[ m1 ];
                              
                                   // dodajemo m u klika
                                   klika01[ m ] = 1;
                                   klika[ len++ ] = m;
                              
                                   //brisemo m iz kandidati_za_swap
                                   k1 = pozicija[ m ];
                                   duljina2--;
                                   n1 = kandidati_za_swap[ duljina2 ];
                                   kandidati_za_swap[ k1 ] = n1;
                                   pozicija[ n1 ] = k1;
                              
                                   for( i = 0; i < broj_nesusjeda[ m ]; i++ )
                                   {
                                      n = matrica[ m ][ i ];
                                      koliko_puta_nesusjed[ n ]++;
                                      if( (koliko_puta_nesusjed[ n ] == 1) && ( klika01[ n ] == 0 ) ) // kao u Add uz jos ovaj drugi uvjet koji nije zadovoljen za vrh m1 jer ga jos nismo izbacili
                                                                                      // (on je jedini nesusjed od m koji je u kliki); za njega ne zelimo ovo provesti
                                      {
                                           k1 = pozicija[ n ];
                                           duljina1--;
                                           n1 = kandidati_za_add[ duljina1 ];
                                           kandidati_za_add[ k1 ] = n1;
                                           pozicija[ n1 ] = k1;
                              
                                           kandidati_za_swap[ duljina2 ] = n;
                                           pozicija[ n ] = duljina2;
                                           duljina2++;
                                           nije_susjed[ n ] = m;
                                      }
                                      if( koliko_puta_nesusjed[ n ] == 2 )
                                      {
                                          duljina2--;
                                          n1 = kandidati_za_swap[ duljina2 ];
                                          k1 = pozicija[ n ];
                                          kandidati_za_swap[ k1 ] = n1;
                                          pozicija[ n1 ] = k1;
                                      }
                                   }
                              
                                   // m1 se brise iz klika i zabranjuje se sve do itreacije koja je zapisana u tabu_lista[m1]
                                   klika01[ m1 ] = 0;
                                   tabu_lista[ m1 ] = iteracija + tabu_tenur + randomInt( duljina2+2 );
                                   len--;
                                   klika[ ti ] = klika[ len ]; // m1 je bio na poziciji ti u klika
                              
                                   // m1 ubacujemo u kandidati_za_swap jer je susjedan sa svima iz klika osim sa m
                                   kandidati_za_swap[ duljina2 ] = m1;
                                   pozicija[ m1 ] = duljina2;
                                   duljina2++;
                              
                                   for( i = 0; i < broj_nesusjeda[ m1 ]; i++ )
                                   {
                                      n = matrica[ m1 ][ i ];
                                      koliko_puta_nesusjed[ n ]--; // oduzimamo jer m1 izbacujemo pa ce n biti nesusjedan jednom vrhu manje iz klike
                                      if( (koliko_puta_nesusjed[ n ] == 0) && (klika01[ n ] == 0) ) // oni koji su prije imali jednog nesusjeda u kliki (sada nemaju nijednog) i koji nisu u kliki;
                                                                                    // onaj za kojeg drugi uvjet propada je m, no njega smo vec izbacili iz kandidati_za_swap i ne zelimo ga dodati u kandidati_za_add
                                      {
                                         k1 = pozicija[ n ];   // one kojima je m1 bio jedini nesusjed u kliki izbacujemo iz kandidati_za_swap
                                         duljina2--;
                                         n1 = kandidati_za_swap[ duljina2 ];
                                         kandidati_za_swap[ k1 ] = n1;
                                         pozicija[ n1 ] = k1;
                              
                                         kandidati_za_add[ duljina1 ] = n;      // dodajemo ih u kandidati_za_add jer su povezani sa svima iz klike
                                         pozicija[ n ] = duljina1;
                                         duljina1++;
                                      }
                                      else if( koliko_puta_nesusjed[ n ] == 1 ) // ovo zadovoljavaju vrhovi koji su prije bili nesusjedni sa dva vrha iz klika, a kako smo sada izbacili vrh m1
                                                                  // iz klika, oni postaju nesusjedi samo sa jednim vrhom iz klika pa ih stoga ubacujemo u kandidati_za_swap
                                      {
                                         kandidati_za_swap[ duljina2 ] = n;
                                         pozicija[ n ] = duljina2;
                                         duljina2++;
                                      }
                                   }
                              
                                   if( tezina_klike > najbolja_tezina )
                                   {
                                       notImproved=0;
                                      najbolja_tezina = tezina_klike;
                                      bestLen = len;
                                      /*for( i = 0; i < broj_vrhova; i++ )
                                          Tbest[ i ] = klika01[ i ];
                                      */
                                   }
                                   notImproved++;
                                   return 1;
                              }
                              
                              
                              int smallestWeightNode()
                              {
                                  int k, l;
                                  int w = 5000000;
                                  l = 0;
                                  for(int i = 0; i < len; i++ )
                                  {
                                     k = klika[ i ];
                                     if( tezine[ k ] < w )
                                     {
                                        l = 0;
                                        w = tezine[ k ];
                                        niz1[ l++ ] = i;
                                     }
                                     else if ( tezine[ k ] <= w )
                                        niz1[ l++ ] = i;
                                  }
                              
                                  if( l == 0 )
                                    return -1;
                                  k = randomInt( l );
                                  k = niz1[ k ];
                                  return k;
                              }
                              
                              int Drop()
                              {
                                   int m, n, k1, n1, x;
                                   x = smallestWeightNode();
                                   if( x == -1 )
                                    return -1;
                                   m = klika[ x ];
                                   //izbacujemo m1 iz klika
                                   tezina_klike = tezina_klike - tezine[ m ];
                                   klika01[ m ] = 0;
                                   tabu_lista[ m ] = iteracija + tabu_tenur;
                                   len--;
                                   klika[ x ] = klika[ len ];
                                   // ubacujemo ga u kandidati_za_add jer je povezan sa svim iz klika
                                   kandidati_za_add[ duljina1 ] = m;
                                   pozicija[ m ] = duljina1;
                                   duljina1++;
                              
                                  // isto kao onaj dio u Swap u kojemu izbacujemo vrh
                                   for(int i = 0; i < broj_nesusjeda[ m ]; i++ )
                                   {
                                      n = matrica[ m ][ i ];
                                      koliko_puta_nesusjed[ n ]--;
                                      if( (koliko_puta_nesusjed[ n ] == 0) && (klika01[ n ] == 0) )
                                      {
                                         k1 = pozicija[ n ];
                                         duljina2--;
                                         n1 = kandidati_za_swap[ duljina2 ];
                                         kandidati_za_swap[ k1 ] = n1;
                                         pozicija[ n1 ] = k1;
                              
                                         kandidati_za_add[ duljina1 ] = n;
                                         pozicija[ n ] = duljina1;
                                         duljina1++;
                                      }
                                      else if( koliko_puta_nesusjed[ n ] == 1 )
                                      {
                                         kandidati_za_swap[ duljina2 ] = n;
                                         pozicija[ n ] = duljina2;
                                         duljina2++;
                                      }
                                   }
                                   notImproved++;
                                   return 1;
                              }
                              
                              int algorithm( int dubina )
                              {
                                   int k, w1, w2, x, y, z, m1;
                                   iteracija = 0;
                                   ocisti();
                                   while( 1 )
                                   {
                                      x = odaberi_random_vrh();
                                      if( x != -1 )
                                      {
                                          Add( x );
                                          iteracija++;
                                      }
                                      else
                                          break;
                                   }
                                   while( iteracija < dubina )
                                   {
                                      x = odaberi_vrh_za_add();
                                      y = odaberi_vrh_za_swap();
                              
                                      if( (x != -1) && (y != -1) )
                                      {
                                          w1 = tezine[ kandidati_za_add[ x ] ];
                                          w2 = tezine[ kandidati_za_swap[ y ] ] - tezine[ nije_susjed[ kandidati_za_swap[ y ] ] ];
                              
                                          if( w1 > w2 )
                                          {
                                              Add( x );
                                              iteracija++;
                                          }
                                          else
                                          {
                                              Swap( y );
                                              iteracija++;
                                          }
                                      }
                                      else if( (x != -1) && (y == -1) )
                                      {
                                           Add( x );
                                           iteracija++;
                                      }
                                      else if( (x == -1) && (y != -1) )
                                      {
                                           z = smallestWeightNode();
                                           m1 = klika[ z ];
                                           w1 = tezine[ kandidati_za_swap[ y ] ] - tezine[ nije_susjed[ kandidati_za_swap[ y ] ] ];
                                           w2 = - tezine[ m1 ];
                                           if( w1 > w2 )
                                           {
                                              Swap( y );
                                              iteracija++;
                                           }
                                           else
                                           {
                                               k = Drop();
                                               if( k == -1 )
                                                   return najbolja_tezina;
                                               iteracija++;
                                           }
                                      }
                                      else if( (x == -1) && (y == -1) )
                                      {
                                           k = Drop();
                                           if( k == -1 )
                                              return najbolja_tezina;
                                           iteracija++;
                                      }
                              
                                   }
                              
                                   return najbolja_tezina;
                              }
                              
                              void verify()
                              {
                                   int i, j;
                                   for( i = 0; i < broj_vrhova; i++ )
                                        if( Tbest[ i ] == 1 )
                                            for( j = i+1; j < broj_vrhova; j++ )
                                                if( (Tbest[ j ] == 1) && ( bridovi[ i ][ j ] == 1 ) )
                                                    cout << "Not a klika." << endl;
                              }
                              /*
                              void Output()
                              {
                                  int i , j, k, l, sum;
                                  FILE *fp ;
                                  int len = strlen(ime_datoteke);
                                  strcpy(outfilename,ime_datoteke) ;
                                  outfilename[len]='.';
                                  outfilename[len+1]='o';
                                  outfilename[len+2]='u';
                                  outfilename[len+3]='t';
                                  outfilename[len+4]='\0';
                              
                                  fp = fopen(outfilename, "a+"); // i za input i za output
                                  for( i = 0; i < 100; i++ ){
                                      fprintf(fp, "sum = %6d, iter = %6d, len = %5d,  time = %8lf \n", W_used[ i ], Iteration[ i ], len_used[ i ],  time_used[ i ] );
                                  }
                              
                                  fprintf(fp, "\n\n the Total information: \n");
                                  int wavg, iteravg, lenbb, success;
                                  wavg = iteravg = lenbb = success = 0;
                                  int best_v = 0;
                                  double timeavg = 0;
                                  for( i = 0; i < 100; i++ )
                                  if( W_used[ i ] > best_v )
                                  {
                                      best_v = W_used[ i ];
                                      lenbb = len_used[ i ];
                                  }
                              
                                  int count = 0;
                                  fprintf(fp, "\n The best weight value for the maximum weighted problem is %6d \n", best_v);
                                  for( i = 0; i < 100; i++ ){
                                     wavg = wavg + W_used[ i ];
                                  }
                                  double twavg = (double (wavg)) / 100 ;
                                  for( i = 0; i < 100; i++ )
                                      if( W_used[ i ] == best_v )
                                      {
                                          count++;
                                          iteravg = iteravg + Iteration[ i ];
                                          timeavg = timeavg + time_used[ i ];
                                      }
                              
                                  iteravg =  int ( (double (iteravg)) / count );
                                  timeavg = timeavg / (count*1000);
                                  fprintf(fp, "avg_sum = %10lf, succes = %6d, len = %5d, avg_iter = %6d,  time = %8lf \n", twavg, count, lenbb,  iteravg, timeavg );
                                  fclose(fp) ;
                                  return ;
                              }
                              */
                              int bestSolutionWeight()
                              {
                                   int l, lbest;
                                   lbest = 0;
                                   Titer = 0;
                                   int M_iter = 0;
                                   start = (double)clock();
                              
                                  //while((double)clock()-start<CLOCKS_PER_SEC){
                                       l = algorithm(4000);
                                       M_iter = M_iter + iteracija;
                                       if( l > lbest )
                                       {
                                         lbest = l;
                                         kraj = (double)clock();
                                         Titer = M_iter;
                                         len_W = bestLen;
                                       }
                                   //}
                                   return lbest;
                              }
                              
                              int main()
                              {
                                  ifstream FIC;
                                  FIC.open("C:/najnoviji/podaci.txt");
                                  if ( FIC.fail() ){
                                      cout << "Greska pri ucitavanju datoteke1.";
                                      exit(0);
                                  }
                                  if ( FIC.eof() ){
                                      cout << "Datoteka je prazna."<< endl;
                                      exit(0);
                                  }
                                  getline(FIC, ime_datoteke);
                                  getline(FIC, ime_datoteke);
                              
                                     srand( (unsigned) time( NULL ) ); //time vraca trenutno vrijeme, a srand inicijalizira generator slucajnog broja
                                     inicijalizacija();
                                     length = broj_vrhova*sizeof( int );
                                     int i, l, maxl;
                                     maxl=0;
                                     for( i = 0; i < 20; i++ )
                                    {
                                        l = bestSolutionWeight();
                                        /*W_used[ i ] = l;
                                        len_used[ i ] = len_W;
                                        time_used[ i ] = kraj - start;
                                        Iteration[ i ] = Titer;
                                        cout << "i = " << i << " l = " << l << endl;*/
                                        if(l>maxl) maxl=l;
                                    }
                                    cout<<maxl<<endl;
                                    //Output();
                              
                                  std::ofstream ofs;
                                  ofs.open("C:/najnoviji/izlaz.txt", std::ofstream::out | std::ofstream::trunc);
                                  ofs.close();
                              
                                  FILE *fp ;
                                  fp = fopen("C:/najnoviji/izlaz.txt", "a+");
                                  fprintf(fp, "%d", maxl);
                                  fclose(fp);
                              }
                              
                              

                              But I'm still left with the same problem from the beginning. The kod.cpp is added in Source directory as SGaits suggested and what I would want from the app is to:

                              • read the current text from two combo boxes, where the first one contains the name of a .cpp file which I want to run (for now only kod.cpp) and the other one contains the .txt file that I want the .cpp file to read (it's some data)),
                              • write them in file 'podaci.txt' in two separate lines
                              • somehow run the .cpp file with line (which is in mainwindow.cpp)
                                QDesktopServices::openUrl(QUrl("file:///C:/najnoviji/"+fileName+".exe",QUrl::TolerantMode));
                              

                              but I'm not sure that this is the way to do it. The .cpp file then writes its result in the file 'izlaz.txt' and the app reads what is in 'izlaz.txt' and writes it in a line edit box. I think this all could be simplified if the .cpp file can just receive arguments from command line (then I would not need the files 'podaci.txt' and 'izlaz.txt'), but I don't know how to connect the app with the .cpp file this way neither.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #20

                                As I wrote before, what you are describing is compiling/linking your kod.cpp before running the resulting binary against your file. Which is likely not what you want to do.

                                Either make it an independent application that handles command line arguments properly and you can pass it the paths to all the necessary files using QProcess or likely simpler: properly integrate the functionality contained in kod.cpp into your GUI application.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                2
                                • M Offline
                                  M Offline
                                  mayathebee
                                  wrote on last edited by
                                  #21

                                  I did it the other way and it seems to be working fine now. Thanks!

                                  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