PDA

View Full Version : ui problem



srohit24
4th August 2009, 04:52
I have two classes that share the same ui.One is the main class called classM and other a sub class of classM called classS.

I am able to effect he ui in classM and get data from the ui(i have a line edit and a few more such input widgets)

But when i try to change the ui from the sublcass classS, it does nothing. It doesnt show any errors eother, but always gives back a blank string for text command in line edit.

This is the header of classM

namespace Ui
{
class classMui;
}
class classM : public QMainWindow
{
Q_OBJECT
private:
Ui::classMui *ui;

This is the header of classS


namespace Ui
{
class classMui;
}
class classS : public QMainWindow
{
Q_OBJECT

private:
Ui::classMui *ui;

ui->passEdit->text();;

I am able to get text for this same code in classM, where as in classS, it gives me a emty string.

what am i doing wrong?

spirit
4th August 2009, 06:05
you creates two different objects of Ui::classMui in a memory. don't mix up classes and objects.

srohit24
4th August 2009, 09:45
ok. So i shouldn't declare classMui in classS??

then how will ui->.... will know which ui to contact?

spirit
4th August 2009, 09:50
ok. So i shouldn't declare classMui in classS??

I did not say that.

if I understand you correct, you need to change ui in one object and get exactly the same changes in another object, right?

srohit24
4th August 2009, 14:43
yup. both the objects should affect the same ui and also should be able to get data from the same ui

spirit
4th August 2009, 14:51
it should not work in that way which you posted (I already said that in my first post), because in this case your forms should have one instance of classMui, but it is bad idea, because when you will call classMui::setupUi for second object that can lead to unexpected behavior.

it's better to use signal/slot mechanism for syncronization your forms.

srohit24
4th August 2009, 17:14
I have a progress bar in the ui.
It will be first set at 10% in the classM, then the execution is handed over to classS where the progress bar moves to 90.

After this it has to get back to classM to complete the execution.

How can set values to the progress bar using the signals??

spirit
4th August 2009, 18:30
make a signal valueChanged in classM then connect that signal with QProgressBar::setValue of classS.

srohit24
5th August 2009, 05:56
I removed all the ui related code and removed the instance of the ui class in classS but still I am still not able to see the progress bar or the status bar in classM.

srohit24
7th August 2009, 15:32
I tried it out. here is my files

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

public slots:
void set_progress_bar();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "class2.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->progressBar->setRange(0,100);

class2 *class2_object = new class2;

connect(class2_object,SIGNAL(change_progress_bar() ), this, SLOT(set_progress_bar()));

}

void MainWindow::set_progress_bar()
{
ui->progressBar->setValue(20);
}

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


class2.h


#ifndef CLASS2_H
#define CLASS2_H

#include <QMainWindow>

class class2 : public QMainWindow
{
Q_OBJECT

public:
class2();
void dummy();

signals:
void change_progress_bar();
};

#endif // CLASS2_H


class2.cpp


#include "class2.h"
#include "mainwindow.h"
#include <QDebug>

class2::class2()
{
dummy();
}

void class2::dummy()
{
qDebug() << "Before signal is sent";
emit (change_progress_bar());
qDebug() << "After signal is sent";
}


This the output i am getting


Before signal is sent
After signal is sent

but the progress bar in the ui, isnt at 20%

what should i change??

spirit
7th August 2009, 15:45
add qDebug here


void MainWindow::set_progress_bar()
{
ui->progressBar->setValue(20);
qDebuig() << "executed";
}

spirit
7th August 2009, 15:47
btw, you send a signal before connection


class2 *class2_object = new class2;//right here you create an object and send a signal
//but the connection is not established yet.

connect(class2_object,SIGNAL(change_progress_bar() ), this, SLOT(set_progress_bar()));//establishing connection

try to do this


class2 *class2_object = new class2;

connect(class2_object,SIGNAL(change_progress_bar() ), this, SLOT(set_progress_bar()));
class2_object->dummy();

srohit24
7th August 2009, 17:57
thanks mate. it helped.

I have a AMD 2800+ processor with 512mb RAM. I am using win xp.

But compiling the simple app as above took about 2 minutes.

Is there any way to increase the speed of compilation?