Qt World Summit: Register Today!

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) :
    ui(new Ui::nacitanie_okno)

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


    delete ui;

    and in funkcie.h is this:

    @bool file_exists(const char * subor)
    if (FILE * sub = fopen(subor, "r"))
    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

    void load();@

    And in mainwindow.cpp

    @void MainWindow::load() {
    QMessageBox msgBox;
    msgBox.setText("The document has been modified.");

    And then i call it from main.cpp

    @ MainWindow w;

    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() {
    // 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?


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

    void MainWindow::do_it(){

    QMessageBox msgBox;
    msgBox.setText("My func.");


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

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    delete ui;


    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    private slots:
    void do_it();

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

    Ui::MainWindow *ui;

    #endif // MAINWINDOW_H@

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

  • Lifetime Qt Champion


    @MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    QTimer::singleShot(0, this, SLOT(do_it()));

Log in to reply