Form load function
-
wrote on 16 Nov 2014, 21:12 last edited by
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
-
wrote on 20 Nov 2014, 08:14 last edited by
[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
-
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.
-
wrote on 21 Nov 2014, 23:05 last edited by
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
-
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.
-
wrote on 24 Nov 2014, 21:35 last edited by
[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! :)
-
QTimer::singleShot is your friend
-
wrote on 25 Nov 2014, 16:25 last edited by
[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();
}
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_OBJECTprivate 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 :)
-
Rather:
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer::singleShot(0, this, SLOT(do_it()));
}@
2/9