Skip to content

General and Desktop

This is where all the desktop OS and general Qt questions belong.
83.4k Topics 456.3k Posts
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    5 Views
    No one has replied
  • Refreshing data using QTimer

    Solved
    5
    0 Votes
    5 Posts
    2k Views
    Jez1337J
    There is some confusion about the correct way to farm jobs out to QThreads so I was hoping somebody would chip in with a bit more detail. You can either do it by subclassing QThread and doing the work in there, or by creating a QObject which does the work and moving it to a vanilla QThread (both ways are shown here). We've plumped for the QObject + vanilla QThread method, which has taken all of an hour and a half to program, so I guess it's not too bad even for developers who don't normally use QThread. Our working solution //MainGuiProgram.cpp MainGuiProgram::MainGuiProgram(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); RefreshDataWorker *worker = new RefreshDataWorker;//our worker class worker->moveToThread(&workerThread);//MainGuiProgram has a private member QThread workerThread connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater); connect(this, &CrawlerManager::triggerWorkerToWork, worker, &RefreshDataWorker::doWork);//MainGuiProgram has a signal which tells the worker class to start connect(worker, &RefreshDataWorker::resultReady, this, &CrawlerManager::updateResults);//Gets results back from the worker when it's finished workerThread.start(); } void MainGuiProgram::connectToDatabase() { //connect to database here, this connection is used by the GUI QTimer *t = new QTimer(this); t->setInterval(1000); connect(t, SIGNAL(timeout()), this, SLOT(refreshGuiData())); t->start(); } void MainGuiProgram::refreshGuiData() { QDateTime nextRefreshTime = QDateTime::fromString("20/07/2021 09:00", "dd/MM/yyyy HH:mm"); //hard coded for example's sake if (QDateTime::currentDateTimeUtc().secsTo(expiry) <= 0 && updateHasHappened == false) { ui.statusLabel->setText("Update in progress!"); ui.statusLabel->repaint(); emit triggerWorkerToWork();//The signal which tells the worker to do its work updateHasHappened = true; } } void MainGuiProgram::updateResults(const QString &result) { ui.statusLabel->setText(result); } //RefreshDataWorker.cpp void RefreshDataWorker::doWork() { //connect to database again here, this connection is used by the worker //and thrown away when the refresh is finished ourDatabaseClass.doStuffWithDatabase(); QString result = "Done!"; emit resultReady(result);//a signal which the Main GUI receives when the result is done } Passing the result back using a signal allows the Main GUI thread to update the user interface, thus avoiding @jsulm 's warning about not touching the GUI from other threads. One important point not shown in this code is that the database connections (MySQL) needed to be named, so that the worker can identify and delete its own connection without mucking up the connection used by the main thread. Solution to our original questions What is the best practice in terms of Qt classes to execute timed events in a separate thread? Create a QObject worker, farm it out to a basic QThread instance, and tell it when to work using signals triggered from a QTimer running in the program's main thread. When trying to do something at a specific time of day in Qt, must one always use QTimer with an interval? Yes, but you can tell the timer to sleep for periods of 1 second at a time in order to not have to constantly poll the system time to know when to start working. If we want different elements of the GUI to update at different times of the day, should we set a separate QTimer for each of them? No. You can use one QTimer, which wakes up every second, and then each time it wakes up inspect the current time to know which jobs to kick off If nobody posts any other comments with respect to the above I'll mark it as the solution later today. Happy days
  • 0 Votes
    12 Posts
    787 Views
    A
    @J-Hilk said in Why QFileDialog::getExistingDirectory returned non native directory?(pcap directory below with '/', not '\')): toNativeSeparators Thanks.That is OK. [image: 7e4a8669-ad24-429d-ba94-7c622b81b191.png] void Text2PcapWidget::on_toolButton_BrowserCapStoragePath_clicked() { QString pcapStoragePath = QFileDialog::getExistingDirectory(this, UI_RES_CONTROL_TEXT2WIDGET_PCAP_DIAG_TITLE, DIAG_DEFAULT_DIR); pcapStoragePath = QDir::toNativeSeparators(pcapStoragePath); if (pcapStoragePath.isEmpty()) { return; } ui->lineEdit_CapStoragePath->setText(pcapStoragePath); }
  • 0 Votes
    2 Posts
    195 Views
    jsulmJ
    @Igor-Yavkin You should ask in that other thread instead of opening a new one. Also: why do you double post same question? https://forum.qt.io/topic/128694/hello-im-facing-with-an-error-fatal-error-openssl-asn1-h-no-such-file-or-directory-include-openssl-asn1-h Closing this one...
  • Qt Creator, debugger, how to change value format

    Solved
    3
    0 Votes
    3 Posts
    383 Views
    SPlattenS
    @J-Hilk Thank you, didn't see that option at first.
  • 0 Votes
    2 Posts
    872 Views
    jsulmJ
    @Igor-Yavkin Did you install OpenSSL dev package? On which platform are you? What are you building?
  • QMultiMedia: A video on the web plays fine on gstreamer, but it gets stuck on Qt.

    Unsolved
    5
    0 Votes
    5 Posts
    342 Views
    W
    @JoeCFD Thank you for your help. Are you playing this video through this http url? The result of my test is that this video can be played normally if downloaded to local. But if you play it through this link, it will only play for a second. I have only tested it on Qt 5.12.2 and 5.12.8.
  • Questions about embedded test in Qt

    Solved
    7
    0 Votes
    7 Posts
    519 Views
    EngelardE
    Found similar problem on this forum, it is unsolved as well. It is silly, first thing what user do when creating Tests - including path to product project, and Qt Creator does not allow that, nonsense...
  • [Solved] Updating QTableView with more rows dynamically

    22
    1 Votes
    22 Posts
    24k Views
    P
    @sami1592 Did you know how to do it? I'm having the same problem.
  • Issue with FTP using QNetworkAccessManager

    Solved
    10
    0 Votes
    10 Posts
    965 Views
    SGaistS
    Yes, it's one way to implement it.
  • LineEdit only allow specific keyPressEvent

    Solved
    2
    0 Votes
    2 Posts
    578 Views
    eyllanescE
    @kocka Change to: if(event->key() == Qt::Key_Left || event->key() == Qt::Key_Right) { QLineEdit::keyPressEvent(event); return; }
  • function return array

    Unsolved
    2
    0 Votes
    2 Posts
    294 Views
    SGaistS
    Hi, QVector<int> generateStuff() { QVector<int> vector {1, 2, 3}; return vector; }; class SomeClass { void setVector(const QVector<int> &vector); void soSomething(); }; int main(int argc, char **argv) { QVector<int> vector = generateStuff(); SomeClass someClass; someClass.setVector(vector); someClass.doSomething(); return 0; }
  • "Not supported" reply while using QNetworkAccessManager Qt5.11.

    Unsolved
    6
    0 Votes
    6 Posts
    360 Views
    SGaistS
    Hi, Check all your pointers. Are you sure you are not triggering a double deletion ? Since you have multiple threads ensure that you are not accessing common data without proper protection in place.
  • How much rows can tableview handle?

    Solved
    4
    0 Votes
    4 Posts
    320 Views
    Thank YouT
    @jsulm Answer is, You can use it for huge amount of data . That are basically needed for quite huge level project with plain tableview. Just tried but result is quite impressing This one is with 50k rows [image: 636cc9b5-0fbe-4b6a-a493-8f95b2536a9c.png] I thought It would crash with more than 100k data I just searched for dummy data and I got. This one is with 100k rows [image: 266dc56d-f990-454f-aff9-d3813bf24609.png] I tried this with local network. It's So fast like it doesn't feel loading This one is with 200k [image: 6d683391-6865-44b7-b899-2a3de55075c1.png] This one with 1300k data Loading takes 2-3 seconds after 1100k data [image: 02af9922-b768-4d40-96aa-673f46e2713c.png] This is with very low end PC Intel Dual Core with 4GB ram If you know more about this
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    6 Views
    No one has replied
  • creating and storing QByteArray from std::vector raw data

    Solved
    6
    0 Votes
    6 Posts
    1k Views
    U
    @jsulm nevermind, i found what i was missing, i bound less parameter than i actually had in my query, so i got that error.
  • How to disable the windows 10 press and hold default behavior on my Qt App ?

    Unsolved
    2
    0 Votes
    2 Posts
    296 Views
    Pl45m4P
    @Amarjit123 Set the contextMenuPolicy on your widget https://doc.qt.io/qt-5/qt.html#ContextMenuPolicy-enum
  • QProcess::startDetached(exe) vs OS system( start exe)

    Solved
    8
    0 Votes
    8 Posts
    573 Views
    H
    @JonB You are right, thanks anyway. Any idea on what the problem might be? Update: I found the problem. The reason it was crashing/freezing was because I was calling deleteLater() on QNetworkReply in the slot connected to finished()
  • BLE bluetooth connection Qt C++

    Unsolved
    2
    0 Votes
    2 Posts
    246 Views
    Pl45m4P
    @ivanicy Sure: https://doc.qt.io/qt-5/qtbluetooth-le-overview.html https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html
  • 0 Votes
    8 Posts
    2k Views
    Pl45m4P
    @sticky-thermos said in Receive Drag/Drop events for items in QGraphicsScene: void ChessBoard::mouseMoveEvent(QMouseEvent *event) { std::cout << FUNCTION << std::endl; QGraphicsView::mousePressEvent(event); QDrag *drag = new QDrag(this); QMimeData *mime = new QMimeData; // this is necessary even if nothing is set in the mime data drag->setMimeData(mime); drag->exec(); } Looks good, but I would add a check to differenciate whether your mouseButton is down. Otherwise you are creating a lot of drags from just moving your mouse around on your chessBoard. Also, you are passing the ChessBoard::mouseMoveEvent(QMouseEvent *event) to QGraphicsView::mousePressEvent(event). QGraphicsView::mouseMoveEvent would be more fitting :)