Results 1 to 4 of 4

Thread: using QThreads, but i cant access a method of another class

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Posts
    20
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default using QThreads, but i cant access a method of another class

    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):

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QThread>
    4. #include "mythread.h"
    5.  
    6.  
    7. int num = 1;
    8. int num2 = 1000000;
    9. int cont = 0;
    10.  
    11. int MyThread::primo(int n){
    12. int i;
    13.  
    14. if( n == 1 ) return 0; // por definicao...
    15.  
    16. for(i=2; i*i <= n;i++){
    17. if (n % i == 0) return 0; // NAO eh primo
    18. }
    19.  
    20. return 1; // EH primo
    21. }
    22.  
    23. MyThread::MyThread(QObject *parent, int num, int num2)
    24. : QThread(parent)
    25. {
    26. this->num = num;
    27. this->num2 = num2;
    28. }
    29.  
    30. void MyThread::run()
    31. {
    32. qDebug() << "Executing a new independant thread, GUI is NOT blocked";
    33.  
    34. int i;
    35. cont = 0;
    36.  
    37. for(i=num; i<num2; i++){
    38. if(primo(i) == 1) cont++; //every time that find it, must refresh the lcdNumber <--------
    39. }
    40.  
    41. qDebug() << "Encontrados:" << cont;
    42.  
    43. //exec();
    44. }
    45.  
    46. int MyThread::retorno(){
    47. return cont;
    48. }
    49.  
    50. MainWindow::MainWindow(QWidget *parent) :
    51. QMainWindow(parent),
    52. ui(new Ui::MainWindow)
    53. {
    54. ui->setupUi(this);
    55.  
    56. QString temp;
    57. temp.setNum(num);
    58. ui->lineEdit->setText(temp);
    59.  
    60. temp.setNum(num2);
    61. ui->lineEdit_2->setText(temp);
    62. }
    63.  
    64. MainWindow::~MainWindow()
    65. {
    66. delete ui;
    67. }
    68.  
    69. void MainWindow::changeEvent(QEvent *e)
    70. {
    71. QMainWindow::changeEvent(e);
    72. switch (e->type()) {
    73. case QEvent::LanguageChange:
    74. ui->retranslateUi(this);
    75. break;
    76. default:
    77. break;
    78. }
    79. }
    80.  
    81. void MainWindow::on_inicio_clicked() //button to start thread
    82. {
    83. QString txt;
    84. txt = ui->lineEdit->text();
    85. num = txt.toInt();
    86. txt = ui->lineEdit_2->text();
    87. num2 = txt.toInt();
    88. lancaThread();
    89. }
    90.  
    91. void MainWindow::on_fim_clicked() //button to terminate thread, if user needs
    92. {
    93. thread->terminate();
    94. qDebug() << "Operacao cancelada";
    95. }
    96.  
    97. void MainWindow::lancaThread()
    98. {
    99. int res = 0;
    100. // create new thread (on heap) and start it
    101. thread = new MyThread(this, num, num2);
    102.  
    103. thread->start(); // after this, thread's run() method starts
    104.  
    105. qDebug() << "Feito";
    106. ui->lcdNumber->display(res);
    107. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by wysota; 14th April 2010 at 20:22. Reason: missing [code] tags

Similar Threads

  1. How to call super class method from sub class
    By mkkguru in forum Qt Programming
    Replies: 9
    Last Post: 4th February 2010, 05:29
  2. QThreads, QMutexes, and multiple access
    By coderbob in forum Qt Programming
    Replies: 3
    Last Post: 12th January 2010, 16:39
  3. QThread Data Access Method
    By schan117 in forum Qt Programming
    Replies: 7
    Last Post: 18th August 2009, 06:25
  4. How to access myApp->method from a child
    By bnilsson in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2008, 13:22
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.