Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

QObject: Cannot create children for a parent that is in a different thread. - Wątki

Ostatnio zmodyfikowano 2013-06-23 14:47
Autor Wiadomość
look997
Temat założony przez niniejszego użytkownika
QObject: Cannot create children for a parent that is in a different thread. - Wątki
» 2013-06-23 13:27:33
Teraz pojawił się problem z samymi wątkami:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QThread(0x28fd88), parent's thread is QThread(0x129209c0), current thread is QThread(0x28fd88)
Jak to naprawić?

C/C++
class Producent
    : public QThread
{
public:
    Producent( int, int, Ui::OknaKlasa * ui );
    void run();
   
public slots:
    void replyFinished( QNetworkReply * );
   
private:
    int n;
    int i;
    Ui::OknaKlasa * ui;
};

Producent::Producent( int, int, Ui::OknaKlasa * ui ) {
    this->n = n;
    this->i = i;
    this->ui = ui;
}

void Producent::run() {
    int ch2 = 1;
    QString sstring = "fd";
   
    QNetworkAccessManager * manager = new QNetworkAccessManager( this );
    connect( manager, SIGNAL( finished( QNetworkReply * ) ),
    this, SLOT( replyFinished( QNetworkReply * ) ) );
   
   
   
    while( true ) {
        qDebug() << ch2++;
        manager->get( QNetworkRequest( QUrl( "https://adr.es/data.json" ) ) );
        //ui->lineEdit->setText(sstring);
        this->msleep( 2000 );
    }
}


void Producent::replyFinished( QNetworkReply * reply ) {
    qDebug() << "DONE";
   
    QJsonDocument document = QJsonDocument::fromJson( reply->readAll() );
    QString string;
   
    //ui->lineEdit->setText( string.setNum(document.object().value( "high" ).toDouble()) ); // przypisanie najwyższej("high") w tym dniu
}

OknaKlasa::OknaKlasa( QWidget * parent )
    : QMainWindow( parent )
    , //, Qt::FramelessWindowHint), ui( new Ui::OknaKlasa )
{
    ui->setupUi( this );
   
    Producent producent1( 123, 1, ui );
    producent1.start();
}
P-85987
Monika90
» 2013-06-23 14:47:22
C/C++
QNetworkAccessManager * manager = new QNetworkAccessManager( this );
Tworzysz obiekt w nowym wątku, a
this
 to wskaźnik do obiektu utworzonego w innym wątku i stąd problem.
P-85990
« 1 »
  Strona 1 z 1