Important: Please read the Qt Code of Conduct -

QThread in MainWindow

  • Hi all,
    I'm trying to write a program for a sound visualization. I want to process reading of samples from a soundcard in one thread and plotting the data in another thread. However, when I want to inherit from QThread in MainWindow, I get an error. So I tried to write another class for plotting to walk around the error from inheriting. But I also get a new error when I try to access ui object outside of MainWindow. What would be a solution to this problem?

    Thank you in advance for your response :)

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    If I understand you correctly you are trying to do something like:

    class MyWindow : public QThread, QMainWindow

    Then you can't. You can't create a subclass from two QObject derived class.

    Anyway, you are trying to mix two things that shouldn't be. Keep responsibilities separated. Do the audio processing in the thread and then use signals and slots to propagate updates to your GUI. That has the added benefit of letting you freely change the GUI without having to rewrite the processing part. You can take some inspiration on e.g. the mandelbrot example for that.

    Hope it helps

  • Thank you so much for your reply SGaist. It helped a lot. I just need to get one more thing clarified. Right now, I am creating an additional thread using my class for audio processing. So that means that I have two threads: the one that deals with the visualization and the one that records the samples. But in my project, I would like to be able to visualize the amplitude and the spectrum at the same time. It means that I will need to update at least two graphic windows in real time. So in addition to what SGaist replied, there is no way in Qt how to separate multiple visualizations into multiple threads when the visualizations happen in a MainWindow?

  • Lifetime Qt Champion

    Have a custom widget for each visualization part and connect them to the corresponding thread. Then put these widgets in your MainWindow e.g. each one in a QDockWidget

Log in to reply