QtConcurrent::mappedReduced - c++
to improve my program, i want to use Qtconcurrent to parallelize my audio process.
Unfortunately, i have many errors and i don't know why ...
In my *.pro : QT += concurrent
I use minGW with Qt 5.3.1 and when i try to build, the compiler said "forming reference to void" (qvector.h).
Here my main code :
void Reduce(QString& result, const QString& txt){ result += txt;}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFuture<QString> res = QtConcurrent::mappedReduced(wavSoundsList, &BenchFFT::process, Reduce, QtConcurrent::OrderedReduce);
res.waitForFinished();
qDebug("%s",qPrintable(res.result()));
return a.exec();
}
and my class :
class BenchFFT
{
public:
BenchFFT();
QString process(const QString &wavFile);
private:
QString soundsPath;
QStringList wavSoundsList;
//Audio file
bool OpenWav(const QString WavPath, SndfileHandle &file);
//FFT
bool ComputeFFT(SndfileHandle file);
double Blackman(int x, int iWindowSize);
//Intel IPP7
IppsFFTSpec_R_64f * pFFTSpec;
Ipp64f* data; // source FFT
Ipp64fc* fftResult; // dest FFT
};
if someone can explain me my mistakes.thx :)
Edit : now i don't have build error with this modification
QFuture<QString> res = QtConcurrent::mappedReduced(wavSoundsList, std::tr1::bind(&BenchFFT::process, BenchFFT(), std::tr1::placeholders::_1), &Reduce, QtConcurrent::OrderedReduce);
but my program crashed .....
Related
Trouble with QT timers: "function definition is not allowed here"
I am trying to make a program that takes images and puts them on your wallpaper using a timer, but I kept getting the error "Timers can only be started with QThread", so I am trying to make this timer with more QThread elements (simpler designs like QThread::msleep haven't worked). Currently, my problem is that my calling slot for when the timer goes off is not working where it currently is, but if I put it in any other location, then the program spits out more errors as it is designed to go in that specific spot. The code itself is mainly a copy/paste of a bunch of other code, and I am new to QT, so I may be going about this completely wrong. If I am, I will gladly accept help so I can understand this better! #include <mainwindow.h> #include <mythread.h> QMediaPlayer * BadAppleS = new QMediaPlayer(); int main(int argc, char *argv[]) { QApplication app(argc,argv); int fileN = 0; BadAppleS->setMedia(QUrl("qrc:/SongN/Bad Apple.mp3")); BadAppleS->play(); mythread t; t.start(); if (fileN <= 1625) { void mythread::doIt(){ //Error here. No more errors elsewhere, though there may be in this function/signal. QString fileNQ = QString::number(fileN); QString filepath = (("qrc:/BAPics/scene (") + fileNQ + (")")); char path[150]; wchar_t wtext[20]; strcpy_s(path, filepath.toStdString().c_str()); mbstowcs(wtext, path, strlen(path)+1); LPWSTR pathp = wtext; int result; result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, pathp, SPIF_UPDATEINIFILE); fileN++; } return app.exec(); } } Thank you for the help!
How to list /dev/sda usb storages mounted with a combobox
I'm looking for the way to show up usb storage path when they are plugged in, the path must be shown in a combobox (in a gui that I'm designing with qt creator (qt 5.9)). I have been searching how to do it but I have not found anything. What I want it's something like: https://catenarios2.files.wordpress.com/2012/11/002.jpg Could you please help me to carry on my project? I would be very grateful if you provide an example. Thank you a lot
The basic idea is the same -- you launch Linux tool via QProcess and parse the result. Here is a simple sketch: #include <QCoreApplication> #include <QProcess> #include <QDebug> #include <usb.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QProcess devList; devList.start("lsblk", QStringList() << "-o" << "KNAME"); if (!devList.waitForStarted()) return false; if (!devList.waitForFinished()) return false; QString result = QString(devList.readAll()); qDebug() << result; return a.exec(); } You can use any other siutable command (quite easy to find them) and should improve parsing, of course, but generally it's all the same. AFAIK, mount points could be obtained from /proc/mounts with something like... #include <QCoreApplication> #include <mntent.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); struct mntent *ent; FILE *aFile; aFile = setmntent("/proc/mounts", "r"); if (aFile == NULL) { perror("setmntent"); exit(1); } while (NULL != (ent = getmntent(aFile))) { printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir); } endmntent(aFile); return a.exec(); } Better than cat launching or someting else, also taken from some snippet and should be improved. And, finally, in case you'll need USD device info, it could be something like... #include <QCoreApplication> #include <usb.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); struct usb_bus *bus; struct usb_device *dev; usb_init(); usb_find_busses(); usb_find_devices(); for (bus = usb_busses; bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { printf("Trying device %s/%s\n", bus->dirname, dev->filename); printf("\tID_VENDOR = 0x%04x\n", dev->descriptor.idVendor); printf("\tID_PRODUCT = 0x%04x\n", dev->descriptor.idProduct); } } return a.exec(); } This needs sudo apt-get libusb-dev + compiling with -lusb. Not really much of Qt in the problem and more fundamental "coding" solutions possible, but hopefully that'll give you a push towards appropriate solution.
how to take a screenshot Within a QT QTest setting
I'm trying to take a screen shot during some tests where the application is being ran on an iOS simulator. The app looks something like this: main.cpp int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; #ifdef TEST thingTests = new ThingTests(&engine); tabletTests->startTestSuite(argv[1]); #endif ... engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); } ThingTests.cpp TabletTests::TabletTests(QQmlApplicationEngine *engine) : QObject(nullptr), m_testSuiteName(NULL) { m_engine = engine; } void TabletTests::startTestSuite(char *testSuiteName) { m_testSuiteName = testSuiteName; connect(m_engine, SIGNAL(objectCreated(QObject*,QUrl)), this, SLOT(onObjectCreated(QObject*,QUrl))); } void TabletTests::onObjectCreated(QObject *, const QUrl &) { // run settings test for now, later on control with command line arguments SettingsTest *settingsTest = new SettingsTest(m_engine); QTest::qExec(settingsTest); } SettingsTest.cpp void SettingsTest::openWarningModeSelectionTest() { m_settingsTester->performTestOperation(SettingsTester::SettingsTestOperation::WarningModeSelection); QTest::qWait(1000); bool optionSelectorDisplayed = DialogController::getInstance()->optionSelectorShown(); // This clip came from here: https://stackoverflow.com/questions/21697185/how-to-take-screenshot-of-qml-application-without-qquickview foreach(QObject* obj, this->m_engine->rootObjects()) { QQuickWindow* window = qobject_cast<QQuickWindow*>(obj); if (window) { QImage image = window->grabWindow(); //<-- This line throws the assertion error } } QVERIFY(optionSelectorDisplayed); } I've inherited this codebase and I'm not very familiar with QT, but I tried to only include relevant things in the above snippets. What I want to be able to do is take some screenshots on what a page looks like at certain times in the tests. When I have TEST defined, my tests for the app go and do their thing, but they blowup when the window->grabWindow() line is hit with an ASSERT error in assert w in scenegraph/qsgthreadedrenderloop.cpp in the qt libraries. This is the assert that is failing (https://code.woboq.org/qt5/qtdeclarative/src/quick/scenegraph/qsgthreadedrenderloop.cpp.html#1281)
Translation not working in Qt
I have been all day long googling for a solution and changing my code, but no luck. Basically, I have added translation to my app. It is working fine except here: QString MainWindow::getMessage(Messages msg) { static const char* const messages[] = { QT_TR_NOOP("Setting power on"), QT_TR_NOOP("Reading ID..."), QT_TR_NOOP("Programming..."), QT_TR_NOOP("Setting write-protect"), QT_TR_NOOP("Finished ok"), QT_TR_NOOP("PROGRAMMED OK"), QT_TR_NOOP("ERROR!"), QT_TR_NOOP("OK"), QT_TR_NOOP("The programmer is not connected.\nPlease check the connection."), QT_TR_NOOP("Disconnect the board, it is in short!!"), QT_TR_NOOP("ERROR: Supply voltage too low (1 Volt is required, Measured: 0.0 Volt)."), QT_TR_NOOP("Board is already programmed and write protected."), QT_TR_NOOP("Check device connection or there is short."), QT_TR_NOOP("Unknown error.") }; return tr(messages[msg]); } However, I don't get the translation. The files for translation seems to be ok, the UI translations are applied fine. I also tried this: static const char* const messages[] = { QT_TRANSLATE_NOOP("test", "Setting power on"), QT_TRANSLATE_NOOP("test", "Reading ID..."), QT_TRANSLATE_NOOP("test", "Programming..."), QT_TRANSLATE_NOOP("test", "Setting write-protect"), QT_TRANSLATE_NOOP("test", "Finished ok"), QT_TRANSLATE_NOOP("test", "PROGRAMMED OK"), QT_TRANSLATE_NOOP("test", "ERROR!"), QT_TRANSLATE_NOOP("test", "OK"), QT_TRANSLATE_NOOP("test", "The programmer is not connected.\nPlease check the connection."), QT_TRANSLATE_NOOP("test", "Disconnect the board, it is in short!!"), QT_TRANSLATE_NOOP("test", "ERROR: Supply voltage too low (1 Volt is required, Measured: 0.0 Volt)."), QT_TRANSLATE_NOOP("test", "Board is already programmed and write protected."), QT_TRANSLATE_NOOP("test", "Check device connection or there is short."), QT_TRANSLATE_NOOP("test", "Unknown error.") }; Then, I have a method to get the message: QString MainWindow::getMessage(Messages msg) { return qApp->translate("test", messages[msg]); } But it doesn't work either. Any tips or suggestions?
I have found the real issue here. Usually translators are installed at main.cpp, so the translator object remains in memory. However, in my case, I was switching the translator after the user changes the language at settings dialog, using a local variable but void QCoreApplication::installTranslator ( QTranslator * translationFile ) [static] needs a pointer. That local variable is removed as soon as the function exits. So, I declared a QTranslator object on my class, so it keeps in memory.
Maybe not applicable in this situation, but often people forget to place the Q_OBJECT macro in the class declaration. Resulting in (amongst others) tr() not working.
I was preparing a simpler source to upload here but now it is working fine! I rebooted my PC yesterday, you know, sometimes a reboot fixes everything (?). Anyway, here is some source as it was requested. And by the way, I'm doing the translation on the fly, and by letting the user to choose the language (not by detecting locales): This is main.cpp (nothing was added by me): #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } This is mainwindow.cpp: #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDir> #include <QTranslator> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QDir d(":/translations/"); QStringList files = d.entryList(QStringList("*.qm")); qDebug("There are %d files for translation", files.count()); // Now let's fill the combobox this->ui->comboBox->clear(); for (int i = 0; i < files.count(); ++i) { QTranslator translator; translator.load(files[i], ":/translations/"); QString language = translator.translate("MainWindow", "English"); this->ui->comboBox->addItem(language); } } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_comboBox_currentIndexChanged(int index) { // Now, based on the new itemindex, let's change the translator QString selectedLang = this->ui->comboBox->itemText(index); QString language; QDir d(":/translations/"); QStringList files = d.entryList(QStringList("*.qm")); for (int i = 0; i < files.count(); ++i) { QTranslator translator; translator.load(files[i], ":/translations/"); language = translator.translate("MainWindow", "English"); if (language == selectedLang) { if (!qApp->installTranslator(&translator)) { qDebug("ERROR INSTALLING TRANSLATOR !!!"); } this->uiTranslate(); break; } } } /// This function translates all the UI texts: void MainWindow::uiTranslate(void) { this->setWindowTitle(tr("MainWindow")); // Just for testing, also show all the messages for (int i = 0; i < MSG_LAST; ++i) { this->ui->textBrowser->append(this->getMessage((Messages)i)); } } QString MainWindow::getMessage(Messages idx) { static const char* const messagesText[MSG_LAST] = { QT_TR_NOOP("Hello!"), QT_TR_NOOP("Bye bye"), QT_TR_NOOP("Nice to meet you") }; return (tr(messagesText[idx])); } in this test app, the UI just has a combobox and a text browser. The combobox is filled with the languages included on the resource file. When I change the combobox, the mainwindow title is changed and the messages are printed in the right language. Thanks anyway! Best regards,
C++ Linking and running LuaJit compiled files with loadbuffer and runbuffer
I have compiled test.lua with LuaJit into test.obj and test.h. How do I correctly use the loadBuffer or runBuffer functions that I have? All I need to find out is basically how to place test.lua, test.obj and test.h into the command but I just cant, Ive tried hundreds of ways but nothing seems to work. I have stripped some other functions off from main and so forth that it would just leave the problem visible and not other things that work just fine. C++: This is the main int main(int argc, const char* argv[]) { std::vector<std::string> args(argv, argv + argc); g_lua.loadBuffer("test.lua", "test.obj") // I have tried both, runBuffer and loadBuffer but I just cant get it right, it always fails. } Here is the loadBuffer function: void LuaCodes::loadBuffer(const std::string& buffer, const std::string& source) { int ret = luaL_loadbuffer(L, buffer.c_str(), buffer.length(), source.c_str()); if(ret != 0) throw LuaException(popString(), 0); } Here is the runBuffer function: void LuaCodes::runBuffer(const std::string& buffer, const std::string& source) { loadBuffer(buffer, source); safeCall(0, 0); } Here are the insides of test.h: #define luaJIT_BC_test_SIZE 1186 static const char luaJIT_BC_test[] = { 27,76,74,1,2,154,9,2,0,12,0,47,0,151,1,52,0,0,0,55,0,1,0,52,1,2,0,55,1,3,1,62, 1,1,2,52,2,4,0,55,2,5,2,62,2,1,2,37,3,6,0,36,1,3,1,62,0,2,1,52,0,0,0,55,0,7,0, 52,1,8,0,55,1,9,1,37,2,10,0,62,1,2,0,61,0,0,1,52,0,0,0,55,0,7,0,52,1,4,0,55,1, 11,1,62,1,1,2,37,2,12,0,52,3,4,0,55,3,13,3,62,3,1,2,37,4,14,0,52,5,4,0,55,5, 15,5,62,5,1,2,37,6,16,0,52,7,4,0,55,7,17,7,62,7,1,2,37,8,18,0,52,9,4,0,55,9, 19,9,62,9,1,2,37,10,20,0,52,11,4,0,55,11,21,11,62,11,1,2,36,1,11,1,62,0,2,1, 52,0,2,0,55,0,22,0,52,1,2,0,55,1,3,1,62,1,1,2,37,2,23,0,36,1,2,1,41,2,2,0,62, 0,3,2,14,0,0,0,84,0,4,128,52,0,0,0,55,0,24,0,37,1,25,0,62,0,2,1,52,0,2,0,55,0, 22,0,52,1,2,0,55,1,3,1,62,1,1,2,37,2,26,0,36,1,2,1,41,2,2,0,62,0,3,2,14,0,0,0, 84,0,4,128,52,0,0,0,55,0,24,0,37,1,27,0,62,0,2,1,52,0,2,0,55,0,22,0,52,1,2,0, 55,1,3,1,62,1,1,2,37,2,28,0,36,1,2,1,41,2,2,0,62,0,3,1,52,0,2,0,55,0,29,0,52, 1,4,0,55,1,5,1,62,1,1,0,61,0,0,1,52,0,2,0,55,0,30,0,37,1,31,0,37,2,32,0,41,3, 2,0,62,0,4,1,52,0,33,0,55,0,34,0,37,1,35,0,62,0,2,1,52,0,36,0,55,0,37,0,62,0, 1,1,52,0,36,0,55,0,38,0,39,1,99,0,62,0,2,1,52,0,36,0,55,0,39,0,37,1,40,0,62,0, 2,1,52,0,36,0,55,0,39,0,37,1,41,0,62,0,2,1,52,0,36,0,55,0,38,0,39,1,243,1,62, 0,2,1,52,0,36,0,55,0,39,0,37,1,42,0,62,0,2,1,52,0,36,0,55,0,38,0,39,1,231,3, 62,0,2,1,52,0,36,0,55,0,39,0,37,1,43,0,62,0,2,1,52,0,36,0,55,0,38,0,39,1,15, 39,62,0,2,1,37,0,31,0,52,1,4,0,55,1,5,1,62,1,1,2,37,2,44,0,36,0,2,0,52,1,2,0, 55,1,45,1,16,2,0,0,62,1,2,2,15,0,1,0,84,2,3,128,52,1,46,0,16,2,0,0,62,1,2,1, 71,0,1,0,11,100,111,102,105,108,101,15,102,105,108,101,69,120,105,115,116,115, 7,114,99,19,103,97,109,101,95,105,110,116,101,114,102,97,99,101,11,99,108,105, 101,110,116,12,103,97,109,101,108,105,98,12,99,111,114,101,108,105,98,23,101, 110,115,117,114,101,77,111,100,117,108,101,76,111,97,100,101,100,20,97,117, 116,111,76,111,97,100,77,111,100,117,108,101,115,20,100,105,115,99,111,118, 101,114,77,111,100,117,108,101,115,14,103,95,109,111,100,117,108,101,115,17, 47,99,111,110,102,105,103,46,111,116,109,108,9,108,111,97,100,14,103,95,99, 111,110,102,105,103,115,11,46,111,116,112,107,103,6,47,25,115,101,97,114,99, 104,65,110,100,65,100,100,80,97,99,107,97,103,101,115,22,115,101,116,117,112, 85,115,101,114,87,114,105,116,101,68,105,114,9,109,111,100,115,56,85,110,97, 98,108,101,32,116,111,32,97,100,100,32,109,111,100,117,108,101,115,32,100,105, 114,101,99,116,111,114,121,32,116,111,32,116,104,101,32,115,101,97,114,99,104, 32,112,97,116,104,46,12,109,111,100,117,108,101,115,53,85,110,97,98,108,101, 32,116,111,32,97,100,100,32,100,97,116,97,32,100,105,114,101,99,116,111,114, 121,32,116,111,32,116,104,101,32,115,101,97,114,99,104,32,112,97,116,104,46, 10,102,97,116,97,108,9,100,97,116,97,18,97,100,100,83,101,97,114,99,104,80,97, 116,104,17,103,101,116,66,117,105,108,100,65,114,99,104,15,32,102,111,114,32, 97,114,99,104,32,17,103,101,116,66,117,105,108,100,68,97,116,101,16,41,32,98, 117,105,108,116,32,111,110,32,19,103,101,116,66,117,105,108,100,67,111,109, 109,105,116,7,32,40,21,103,101,116,66,117,105,108,100,82,101,118,105,115,105, 111,110,10,32,114,101,118,32,15,103,101,116,86,101,114,115,105,111,110,6,32, 12,103,101,116,78,97,109,101,42,61,61,32,97,112,112,108,105,99,97,116,105,111, 110,32,115,116,97,114,116,101,100,32,97,116,32,37,98,32,37,100,32,37,89,32,37, 88,9,100,97,116,101,7,111,115,9,105,110,102,111,9,46,108,111,103,19,103,101, 116,67,111,109,112,97,99,116,78,97,109,101,10,103,95,97,112,112,15,103,101, 116,87,111,114,107,68,105,114,16,103,95,114,101,115,111,117,114,99,101,115,15, 115,101,116,76,111,103,70,105,108,101,13,103,95,108,111,103,103,101,114,0 };
For luaL_loadbuffer (and hence LuaCodes::loadBuffer) the 1st argument should be a string containing the bytecode and the 2nd argument should be a human-readable name (e.g. the filename that the bytecode was compiled from.) Try: #include "test.h" // ... int main(int argc, const char* argv[]) { // ... std::string bytecode(luaJIT_BC_test, luaJIT_BC_test_SIZE); g_lua.loadBuffer(bytecode, "#test.lua") }