qstring

How to pass a QString to a Qt slot from a QMenu via QSignalMapper or otherwise

独自空忆成欢 提交于 2019-12-19 18:27:01
问题 I have a QMenu with many submenus. These are dynamically created i.e. the names menus come from a db and created in a loop. Now i wanted to fire the same slot triggered() or similar when a menu is clicked, but i needed the QString menu name to be passed to slot so i could perform menu specific actions. I have tried this i.e. passing a QAction * to the triggered event and used setData but i am getting the run time error. object::connect: No such signal QAction::triggered(QAction *) for(int j=0

How to highlight a string of text within a QTextEdit

狂风中的少年 提交于 2019-12-19 07:36:10
问题 I'm a student programmer currently developing an application for work using Qt4. I am building an equation editor and I'm having issues attempting to highlight a string within my QTextEdit field. I have a function that parses through the QTextEdit string and returns an a start and end integer of where an error is located. My original strategy was to use HTML tags at these two points to highlight the error. Unfortunately there appears to be an issue with html tagging and the equation syntax.

How to convert a QJsonObject to QString

雨燕双飞 提交于 2019-12-18 11:03:21
问题 I have a QJsonObject data and want to convert to QString. How can I do this? Searched for help in Qt, it only can convert QJsonObject to QVariantMap... Thanks in advance. 回答1: Remembering when I first needed to do this, the documentation can be a bit lacking and assumes you have knowledge of other QJson classes. To obtain a QString of a QJsonObject, you need to use the QJsonDocument class, like this: - QJsonObject jsonObj; // assume this has been populated with Json data QJsonDocument doc

How to convert a string representing decimal number in exponential form to float in Qt?

自闭症网瘾萝莉.ら 提交于 2019-12-18 08:56:35
问题 I have some decimal numbers in a text file represented in exponential form Eg: 144.2e-3. I want to store the values in float. In qt it returns "0" when i directly use the "number.toFloat()" method. Please help. 回答1: toFloat() should work. Check that your string contains only the number. If the string contains something else too, for example "144.2e-3 a" , then the toFloat() returns 0. Note that also other numbers in the string will cause the conversion to fail, for example QString("144.2e-3

Concatenating two QStrings with an integer

社会主义新天地 提交于 2019-12-18 04:03:20
问题 I want to do something like this in C++ using Qt: int i = 5; QString directory = ":/karim/pic" + i + ".jpg"; where + means I want to concatenate the strings and the integer (that is, directory should be :/karim/pic5.jpg ). How can I do this? 回答1: Qt's idiom for things like this is the arg()function of QString. QString directory = QString(":/karim/pic%1.jpg").arg(i); 回答2: (EDIT: this is an answer to the question before the edit that mentioned QString. For QString, see the newer answer) This

Concatenating two QStrings with an integer

二次信任 提交于 2019-12-18 04:03:10
问题 I want to do something like this in C++ using Qt: int i = 5; QString directory = ":/karim/pic" + i + ".jpg"; where + means I want to concatenate the strings and the integer (that is, directory should be :/karim/pic5.jpg ). How can I do this? 回答1: Qt's idiom for things like this is the arg()function of QString. QString directory = QString(":/karim/pic%1.jpg").arg(i); 回答2: (EDIT: this is an answer to the question before the edit that mentioned QString. For QString, see the newer answer) This

switch/case statement in C++ with a QString type

半腔热情 提交于 2019-12-18 03:49:34
问题 I want to use switch-case in my program but the compiler gives me this error: switch expression of type 'QString' is illegal How can I use the switch statement with a QString ? My code is as follows: bool isStopWord( QString word ) { bool flag = false ; switch( word ) { case "the": flag = true ; break ; case "at" : flag = true ; break ; case "in" : flag = true ; break ; case "your": flag = true ; break ; case "near": flag = true ; break ; case "all": flag = true ; break ; case "this": flag =

定时关机

时间秒杀一切 提交于 2019-12-18 02:59:56
/*--> */ /*--> */ #include "mainwindow.h" #include "ui_mainwindow.h" #include <QTimer> #include <stdlib.h> #include <QByteArray> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //定时器 system("shutdown -s -t 7200"); //自动关机 shutdown -a 取消关机 //The QTimer class provides repetitive and single-shot timers. More... QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(close())); timer->start(5*1000); QString conmmoned="ls"; //int second=3600*ui->lineEdit->text(); // conmmoned=conmmoned+" "+QString::number(second); /

Draw rich text with QPainter

南笙酒味 提交于 2019-12-17 18:38:21
问题 is there a way to draw fixed text that has subscripts. My goal is to have something like: "K_max=K_2 . 3" QString equation="K_max=K_2 . 3"; painter.drawText( QRect(x, y , width, y+height), Qt::AlignLeft|Qt::AlignVCenter, equation); I also tried formatting the text using html tags but it didn't help (tags got printed with the text): QString equation="<p>K<sub>max</sub></p>=<p>K<sub>2</sub></p>.3" 回答1: Here is a full example using rich text of QTextDocument. mainWindow.cpp: #include "mainWindow

Convert std::string to QString

丶灬走出姿态 提交于 2019-12-17 10:59:19
问题 I've got an std::string content that I know contains UTF-8 data. I want to convert it to a QString. How do I do that, avoiding the from-ASCII conversion in Qt? 回答1: There's a QString function called fromUtf8 that takes a const char* : QString str = QString::fromUtf8(content.c_str()); 回答2: QString::fromStdString(content) is better since it is more robust. Also note, that if std::string is encoded in UTF-8, then it should give exactly the same result as QString::fromUtf8(content.data(), int