Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[CLOSED] catch signals from a static library



  • i have a static library called "Test" in which below function exists:

    @
    Test.h
    #include <QtCore>
    #include <QObject>

    class Test:public QObject
    {
    Q_OBJECT

    public:
    Test(QObject *parent=0) : QObject(parent) { }
    void getMessage(QString &strMessage);

    signals:
    void finished(int);
    };

    Test.cpp
    void Test::getMessage(QString &strMessage)
    {
    strMessage = "Hello world!";
    // emit finished(0); if i uncomment this line i get segFualt in caller thread
    }

    my application main func:
    int main(int argc, char *argv[])
    {
    QString parameter;
    DWORD dw1;
    HANDLE hdl;
    Test obj;
    // ok is true after below code
    bool ok = QObject::connect(&obj, SIGNAL(finished(int)), this, SLOT(mySlot(int)));

    hdl = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &Test::getMessage, &parameter, 0, &dw1);
    if(hdl == NULL)
    {
        qDebug() << "failed to create thread.";
    }
    // if i uncomment line "emit finished(0);" i get sefFault here and if i don't app works perfectly
    WaitForSingleObject(hdl,INFINITE);
    if(parameter.isEmpty())
    {
        qDebug() << "Parameter failed.";
    }
    
    return a.exec(&#41;;
    

    }
    @
    i run a function from lib in another thread i want that function to emit a signal at the end and catch that signal in my app.


  • Moderators

    [quote]
    @
    int main(int argc, char *argv[])
    {
    ...
    bool ok = QObject::connect(&obj, SIGNAL(finished(int)), this, SLOT(mySlot(int)));
    }
    @
    [/quote]main() doesn't have a this pointer. Where is this connection actually done?

    Also, what do you want to do with your thread? If you need threads, I recommend using one of Qt's cross-platform thread solutions: http://doc-snapshot.qt-project.org/qt5-stable/threads-technologies.html



  • [quote author="JKSH" date="1383120515"][quote]
    @
    int main(int argc, char *argv[])
    {
    ...
    bool ok = QObject::connect(&obj, SIGNAL(finished(int)), this, SLOT(mySlot(int)));
    }
    @
    [/quote]main() doesn't have a this pointer. Where is this connection actually done?

    Also, what do you want to do with your thread? If you need threads, I recommend using one of Qt's cross-platform thread solutions: http://doc-snapshot.qt-project.org/qt5-stable/threads-technologies.html[/quote]

    yes u are right actually the code inside main is in another function it was a copy and paste mistake!
    at first i was using QThread but it seems Qthread has some problem on machines with 1core!! i tested this on a VM.


  • Moderators

    [quote author="XGuy" date="1383123159"]yes u are right actually the code inside main is in another function it was a copy and paste mistake![/quote]Can you share your actual code?

    [quote]at first i was using QThread but it seems Qthread has some problem on machines with 1core!! i tested this on a VM.[/quote]What kind of problem?



  • [quote author="JKSH" date="1383128954"][quote author="XGuy" date="1383123159"}yes u are right actually the code inside main is in another function it was a copy and paste mistake![/quote]Can you share your actual code?

    [quoteat first i was using QThread but it seems Qthread has some problem on machines with 1core!! i tested this on a VM.[/quote]What kind of problem?

    [/quote]

    just create a program in which some threads are created and then run it on a machine which has only one core.


  • Moderators

    You said it "has some problem". Can you give more details? Threads work on single-core computers too.

    Can you post the correct code for your original post?



  • [quote author="JKSH" date="1383197446"]# You said it "has some problem". Can you give more details? Threads work on single-core computers too.

    Can you post the correct code for your original post?[/quote]

    in my case when i run multiple threads on a machine with one core the threads don't run simultaneously, it seems QThread runs each thread in a separate core.
    and unfortunately i have changed my source code so the implementation with QThread is not available anymore :-(



  • This used to be my approach for using QThread!

    @
    // objConnLable is a class obj
    objConnLable.moveToThread(&thrConnLabel);
    QObject::connect(&thrConnLabel, SIGNAL(started()), &objConnLable, SLOT(getTokenConnLabel()));
    QObject::connect(&objConnLable, SIGNAL(connLabelReady(QString)), this, SLOT(updateConnList(QString)));
    QObject::connect(&objConnLable, SIGNAL(connLabelFinished(int)), this, SLOT(getConnLabelFinished(int)));
    thrConnLabel.start();
    @



  • Can you post the correct code for your original post?[/quote]

    [quote author="JKSH" date="1383197446"]# You said it "has some problem". Can you give more details? Threads work on single-core computers too.

    Can you post the correct code for your original post?[/quote]

    here is a sample app that shows this.
    i have two threads in which they start simultaneously and they have to update the UI.

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

    model1 = new QStandardItemModel();
    model2 = new QStandardItemModel();
    
    QObject::connect(this, SIGNAL(model1Ready(QStandardItemModel*)), this, SLOT(setdatatoview1(QStandardItemModel*)));
    QObject::connect(this, SIGNAL(model2Ready(QStandardItemModel*)), this, SLOT(setdatatoview2(QStandardItemModel*)));
    

    }

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

    void MainWindow::on_btnstart_clicked()
    {
    QObject::connect(&threadtest1, SIGNAL(started()), this, SLOT(fillModel1()));
    QObject::connect(&threadtest2, SIGNAL(started()), this, SLOT(fillModel2()));
    threadtest1.start();
    threadtest2.start();
    threadtest1.wait(1);
    threadtest2.wait(1);
    }

    void MainWindow::setdatatoview1(QStandardItemModel *model)
    {
    ui->lv1->setModel(model);
    }

    void MainWindow::setdatatoview2(QStandardItemModel *model)
    {
    ui->lv2->setModel(model);
    }

    void MainWindow::fillModel1()
    {
    for(int i = 0; i < 10000; i++)
    {
    QStandardItem *item = new QStandardItem(QString("Item%1").arg(i));
    model1->appendRow(item);
    emit model1Ready(model1);
    }
    }

    void MainWindow::fillModel2()
    {
    for(int i = 0; i < 10000; i++)
    {
    QStandardItem *item = new QStandardItem(QString("Item%1").arg(i));
    model2->appendRow(item);
    emit model2Ready(model2);
    }
    }
    @
    but if u run this, one of the QTableWidgets waits for the other thread to get finished then starts to fill


Log in to reply