PDA

View Full Version : GUI crash using threads



mairimmart
13th November 2013, 12:21
Hello,
I am doing a programme which has to read continously information from a bus, and depending on what it read it will have to show some buttons and labels or anothers in the interface. But, I don't know why the GUI don't have time to refresh, or I don't know what is happening, in any case the GUI don't seem to change but variables did. Could you please give me a piece of advice or a suggestion? Sorry for my poor english. Thank you a lot in advance.
Here is a piece of code I think it's representative of the problem.

MainWindow.h:
#INCLUDES...
using namespace std;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
class Thread *comunicacio;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void Emissio_estat(Pantalles estat_emes);

private:
Ui::MainWindow *ui;

};

MainWindow.cpp:

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

qRegisterMetaType<Pantalles>();
Thread* comunicacio = new Thread(this);

QObject::connect(comunicacio, SIGNAL(Emissio_estat(Pantalles)), this,
SLOT(Emissio_estat(Pantalles)),Qt::QueuedConnectio n);

comunicacio->start(QThread::LowPriority);
}

void MainWindow::Emissio_estat(Pantalles estat_emes){
int estat = estat_emes;
switch (estat){

case XXX:
I use the functions PushButton.show(); Label.hide(); and others...

break;

case XXX: ....

break;

default:

break;
}

Thread.h:
class Thread : public QThread
{
Q_OBJECT
public:
explicit Thread(QObject *parent = 0);
Thread *comunicacio;
QMessageBox *informacio_1, *informacio_2;


protected:
virtual void run();

signals:
void Emissio_estat(Pantalles);
};


Thread.cpp:

void CAN_Thread::run(){

DECLARATION & INITIALIZATION OF VARIABLES;

forever{
nbytes = read(data);

if (data != 0x00)
{

switch (data)
{
case xxxx:
emit Emissio_estat(Pantalles);
break;
case xxxx:
emit Emissio_estat(Pantalles);
break;
default:
break;
}
}
}
}

anda_skoa
13th November 2013, 13:51
That looks kind of OK.
Can you make a qDebug() output in MainWindow::Emissio_estat() and see if it gets called?

Btw, the Forum has code tags for posting code snippets. code and /code in square brackets

Cheers,
_

mairimmart
27th November 2013, 13:12
Thank you very much for your replay, and sorry for the late of mine.
Finally I've fixed the mistake, it was about another part of the code...
Eventhough thanks a lot! :p