Create signal and slot to detect change in QString object

  • I am new to Qt development, and I am learning a lot. I am also learning a lot about C++ programming over and above what I learned in college.

    I am working on a program where I have gotten a list of files in a directory (QStringList). I then use a QThread::create(functionName, <parameters>) function to create a thread to get a single file name with a brief delay using the (QThread sleep function) and put it into a pointer to a QString variable that was declared in the main thread (MainWindow).

    Now that I have the file name in the main thread I would like to detect when this QString variable changes so I can do a few thing to a couple UI elements related to the value in this variable.

    I have spent a couple of three days searching the internet and reading. Most of what is available reads almost alike if not exactly alike. There is also a lot of old material out there.

    The typical signal and slot is in class form where the slot is contained within the class, from what I am able to gather, contained within a thread out of reach of the main thread where the UI elements are.

    I have tried starting second thread with a loop comparing the string stored in the variable against a variable that stores the 'old name'. There doesn't seem to be any way to emit some kind of signal to outside the thread to indicate when the QString variable in question has changed.

    Is there a way to get a thread to terminate itself?

    Any assistance will be greatly appreciated.

  • @Walt-Williams said in Create signal and slot to detect change in QString object:

    There doesn't seem to be any way to emit some kind of signal to outside the thread to indicate when the QString variable in question has changed.

    No there isn't any signal to directly detect changes made to a QString.

    You are problably following a wrong approach.
    Better track the way (how your string gets changed) than the string itself.
    If you read-in some files and save the paths in a QStringList, the strings can only change if you read your files again.
    So you could make a simple check before assigning the value. If currentString == newString -> no change. Otherwise your string has changed.

    But maybe you could be a little more accurate, what exactly you are trying to do and what you have so far (code)...

  • You seem to be thinking in sequential programming terms, and not understanding event driven programming. Everything that happens in the Qt framework is based on an event delivered to an event processing loop. Also, your use of a global object pointer to communicate with thread objects (while suitable when semaphore locked in pure std:: c++) is very dangerous in Qt. You should always use the signal/slot mechanism to communicate between classes that are derived from QObject. You should emit(QString) from your thread class and connect() that signal to a slot in your mainwindow. If your main window code is uneditable, then you subclass your main window, and add the required slot to the subclass.

  • Thank you for the replays.

    I got early this morning and found the correct way of doing what I wanted by setting up a thread with signal and slot. It involves writing a class that inherits from QObject and communicates back to the main thread through another signal and slot (see below).

    QObject::connect(get_the_filename, SIGNAL(filename_signal(QString)), this, SLOT(on_receiving_filename(QString)));

    Even though its working Qt-Creator is currently complaining about the line above stating:

    QMetaObject::connectSlotsByName: No matching signal for on_receiving_filename(QString)

    Creator doesn't seem to be able to detect that I am calling a SIGNAL in an object placed inside of a thread. My MainWindow class currently doesn't have any slots.

  • Did you declare the signal as such in the header file:


    Also look at the updated format for connect().

  • @Walt-Williams

    get_the_filename (first argument in connect statement) doesn't sound like the name of your sender object / instance. Is this a function?!

Log in to reply