Tio
14th April 2010, 14:12
hello everyone
i am new here, so if i've made something wrong, my apologizes ^^
Plus, i dont speak english a very well, so, i am sorry for wrong write =P
well...i have a problem.
I am using the QThreads to launch some threads. Basicaly, i have 2 classes: MainWindow, which is responsible for the window, and MyThread, for one thread.
I am going a very well, launch a thread and every else. But now, i need access some resources of the MainWindow from the MyThread. Actually, i have on the window a lcdNumber, which must be refreshed every number found in the thread. Someone knows how to make it?
here is the code (comments is in portuguese, my language, didnt mind =P):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include "mythread.h"
int num = 1;
int num2 = 1000000;
int cont = 0;
int MyThread::primo(int n){
int i;
if( n == 1 ) return 0; // por definicao...
for(i=2; i*i <= n;i++){
if (n % i == 0) return 0; // NAO eh primo
}
return 1; // EH primo
}
MyThread::MyThread(QObject *parent, int num, int num2)
: QThread(parent)
{
this->num = num;
this->num2 = num2;
}
void MyThread::run()
{
qDebug() << "Executing a new independant thread, GUI is NOT blocked";
int i;
cont = 0;
for(i=num; i<num2; i++){
if(primo(i) == 1) cont++; //every time that find it, must refresh the lcdNumber <--------
}
qDebug() << "Encontrados:" << cont;
//exec();
}
int MyThread::retorno(){
return cont;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString temp;
temp.setNum(num);
ui->lineEdit->setText(temp);
temp.setNum(num2);
ui->lineEdit_2->setText(temp);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_inicio_clicked() //button to start thread
{
QString txt;
txt = ui->lineEdit->text();
num = txt.toInt();
txt = ui->lineEdit_2->text();
num2 = txt.toInt();
lancaThread();
}
void MainWindow::on_fim_clicked() //button to terminate thread, if user needs
{
thread->terminate();
qDebug() << "Operacao cancelada";
}
void MainWindow::lancaThread()
{
int res = 0;
// create new thread (on heap) and start it
thread = new MyThread(this, num, num2);
thread->start(); // after this, thread's run() method starts
qDebug() << "Feito";
ui->lcdNumber->display(res);
}
the last line is, basically, what i need to do up there. The "ui" is public, but i cant access. ive tried to use:
MainWindow::ui->lcdNumber->display(anumber);
but didnt work...
if someone could help me, i will be very grateful ^^
if needs more information, just ask =D
and sorry for anything
i am new here, so if i've made something wrong, my apologizes ^^
Plus, i dont speak english a very well, so, i am sorry for wrong write =P
well...i have a problem.
I am using the QThreads to launch some threads. Basicaly, i have 2 classes: MainWindow, which is responsible for the window, and MyThread, for one thread.
I am going a very well, launch a thread and every else. But now, i need access some resources of the MainWindow from the MyThread. Actually, i have on the window a lcdNumber, which must be refreshed every number found in the thread. Someone knows how to make it?
here is the code (comments is in portuguese, my language, didnt mind =P):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include "mythread.h"
int num = 1;
int num2 = 1000000;
int cont = 0;
int MyThread::primo(int n){
int i;
if( n == 1 ) return 0; // por definicao...
for(i=2; i*i <= n;i++){
if (n % i == 0) return 0; // NAO eh primo
}
return 1; // EH primo
}
MyThread::MyThread(QObject *parent, int num, int num2)
: QThread(parent)
{
this->num = num;
this->num2 = num2;
}
void MyThread::run()
{
qDebug() << "Executing a new independant thread, GUI is NOT blocked";
int i;
cont = 0;
for(i=num; i<num2; i++){
if(primo(i) == 1) cont++; //every time that find it, must refresh the lcdNumber <--------
}
qDebug() << "Encontrados:" << cont;
//exec();
}
int MyThread::retorno(){
return cont;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString temp;
temp.setNum(num);
ui->lineEdit->setText(temp);
temp.setNum(num2);
ui->lineEdit_2->setText(temp);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_inicio_clicked() //button to start thread
{
QString txt;
txt = ui->lineEdit->text();
num = txt.toInt();
txt = ui->lineEdit_2->text();
num2 = txt.toInt();
lancaThread();
}
void MainWindow::on_fim_clicked() //button to terminate thread, if user needs
{
thread->terminate();
qDebug() << "Operacao cancelada";
}
void MainWindow::lancaThread()
{
int res = 0;
// create new thread (on heap) and start it
thread = new MyThread(this, num, num2);
thread->start(); // after this, thread's run() method starts
qDebug() << "Feito";
ui->lcdNumber->display(res);
}
the last line is, basically, what i need to do up there. The "ui" is public, but i cant access. ive tried to use:
MainWindow::ui->lcdNumber->display(anumber);
but didnt work...
if someone could help me, i will be very grateful ^^
if needs more information, just ask =D
and sorry for anything