Form load function



  • 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



  • [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


  • Lifetime Qt Champion

    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.



  • 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


  • Lifetime Qt Champion

    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 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! :)


  • Lifetime Qt Champion

    QTimer::singleShot is your friend



  • [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 :)


  • Lifetime Qt Champion

    Rather:

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


Log in to reply
 

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