Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [CLOSED] catch signals from a static library

[CLOSED] catch signals from a static library

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    XGuy
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      [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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • X Offline
        X Offline
        XGuy
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          [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?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • X Offline
            X Offline
            XGuy
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              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?

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • X Offline
                X Offline
                XGuy
                wrote on last edited by
                #7

                [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 :-(

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  XGuy
                  wrote on last edited by
                  #8

                  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();
                  @

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    XGuy
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved