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. Form load function
Forum Updated to NodeBB v4.3 + New Features

Form load function

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 5.6k Views 1 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.
  • T Offline
    T Offline
    t0msk
    wrote on 16 Nov 2014, 21:12 last edited by
    #1

    i created new form in Qt and i would like to ask you where is form load function, where i can put my code.

    And another problems are that file_exists doesn't work and i dont know why (i would like to use C native functions), and my Messagebox show before form load why? I would like to load while form and then show my Messagebox. And the last thing is that this->close(); at if statement doesn't work.

    This is my code:

    @#include "nacitanie_okno.h"
    #include "ui_nacitanie_okno.h"
    #include "funkcie.h"
    #include <iostream>

    const char *subory[] = { "test.txt" } ;

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

    int i;
    int pocet = 1;
    
    int percent = 20 / pocet;
    for(i = 0; i < pocet ; i++){
    
        if(file_exists(subory[i])){
            ui->progressBar->setValue(ui->progressBar->value() + percent);
        } else {
            MessageBox("Hi","teeest"); // my own function for messagebox
            this->close();
        }
    }
    

    }

    nacitanie_okno::~nacitanie_okno()
    {
    delete ui;
    }@

    and in funkcie.h is this:

    @bool file_exists(const char * subor)
    {
    if (FILE * sub = fopen(subor, "r"))
    {
    fclose(sub);
    return true;
    }
    return false;
    }@

    thank you

    Student who loves C/C++

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t0msk
      wrote on 20 Nov 2014, 08:14 last edited by
      #2

      [quote author="SGaist" date="1416439024"]You can't but C++ code anywhere in a file like that.

      Also, Afterload doesn't belong to MainWindow.

      Are you a C++ beginner ?[/quote]

      At C++ yes, but i have experiences with C and C#. So how does it work? Where i have to put it? :)

      Thank you

      Student who loves C/C++

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 20 Nov 2014, 23:54 last edited by
        #3

        in e.g. main.cpp

        You should take the time to go through Qt's tutorial and read some of the examples/demos provided with Qt's sources.

        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
        • T Offline
          T Offline
          t0msk
          wrote on 21 Nov 2014, 23:05 last edited by
          #4

          Okey i found solution.

          I just add to mainwindow.h

          @public:
          void load();@

          And in mainwindow.cpp

          @void MainWindow::load() {
          QThread::msleep(500);
          QMessageBox msgBox;
          msgBox.setText("The document has been modified.");
          msgBox.exec();
          }@

          And then i call it from main.cpp

          @ MainWindow w;
          w.show();
          w.load();@

          This works but it amazes me that such an important function is not implemented in default "Slots" (Go to slot) in Qt Creator :)

          Thank you

          Student who loves C/C++

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 22 Nov 2014, 23:20 last edited by
            #5

            Important in your you ;)

            There are several possibilities to achieve that and without knowing what exactly you'd like to do after the widget is shown, it's not easy to help you.

            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
            • T Offline
              T Offline
              t0msk
              wrote on 24 Nov 2014, 21:35 last edited by
              #6

              [quote author="SGaist" date="1416698455"]Important in your you ;)

              There are several possibilities to achieve that and without knowing what exactly you'd like to do after the widget is shown, it's not easy to help you.[/quote]
              I have one more question how can i call function after all items in widgets are constructed? (By items i mean for example, buttons, progress bar etc). Because when i write:

              @void MainWindow::load() {
              QThread::sleep(1);
              // my func
              }@

              then i have to wait 1 second + execution time of my func to load all items in widget. I would like to construct all items and then call my func.

              Can you help me please?

              Thank you very much! :)

              Student who loves C/C++

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 25 Nov 2014, 00:49 last edited by
                #7

                QTimer::singleShot is your friend

                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
                1
                • T Offline
                  T Offline
                  t0msk
                  wrote on 25 Nov 2014, 16:25 last edited by
                  #8

                  [quote author="SGaist" date="1416876558"]QTimer::singleShot is your friend[/quote]

                  Do you mean this?

                  mainwindow.cpp

                  @#include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include <QMessageBox>
                  #include <QTimer>
                  #include <QThread>

                  void MainWindow::do_it(){

                  QMessageBox msgBox;
                  msgBox.setText("My func.");
                  msgBox.exec&#40;&#41;;
                  

                  }

                  void MainWindow::Afterload(){
                  QTimer::singleShot(500, this, SLOT(do_it()));
                  }

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

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

                  mainwindow.h

                  @#ifndef MAINWINDOW_H
                  #define MAINWINDOW_H

                  #include <QMainWindow>

                  namespace Ui {
                  class MainWindow;
                  }

                  class MainWindow : public QMainWindow
                  {
                  Q_OBJECT

                  private slots:
                  void do_it();

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

                  private:
                  Ui::MainWindow *ui;
                  };

                  #endif // MAINWINDOW_H@

                  This works, but i am wondering if is this correct :)

                  Student who loves C/C++

                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 26 Nov 2014, 00:01 last edited by
                    #9

                    Rather:

                    @MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                    {
                    ui->setupUi(this);
                    QTimer::singleShot(0, this, SLOT(do_it()));
                    }@

                    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
                    1

                    1/9

                    16 Nov 2014, 21:12

                    • Login

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