double x = 12.3456789;
double y = 98.7654321;
QString str = QString("x: %1 y: %2").arg(x, 0, 'f', 3).arg(y, 0, 'f', 3);
OUT:
x: 12.346 y: 98.765
Как применять алгоритмы STL в Qt
To iterate over a list, you can either use index positions or QList's Java-style and STL-style iterator types:
аналог Map из JavaScript
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
template<typename Key, typename Val>
class Map
{
map<Key, Val> values;
vector<Key> index;
public:
// ...
auto operator[](int i)
{
return values[index[i]];
}
// ...
void insert(const pair<Key, Val>& val)
{
values[val.first] = val.second;
index.push_back(val.first);
}
// ...
};
int main()
{
Map<int, string> mp;
mp.insert({3, "three"});
mp.insert({42, "forty two"});
mp.insert({2, "two"});
mp.insert({4, "four"});
for(int i = 0; i < 4; ++i)
{
cout << mp[i] << "\n";
}
}
какой компилятор из списка выбрать?
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "memdb");
db.setDatabaseName(":memory:");
if (!db.open())
{
qDebug() << db.lastError().text();
// std::exit(1);
}
QSqlQuery q("", db);
q.exec("create table Universities (id integer primary key, Location varchar, Population integer)");
q.exec("insert into Universities values (0, 'MSK', 12000000)");
q.exec("insert into Universities values (1, 'NSK', 1600000)");
q.exec("insert into Universities values (2, 'SPB', 6000000)");
q.exec("insert into Universities values (3, 'PRM', 1000000)");
int sz = ui->tableWidget->rowCount();
QString queryString = "SELECT * FROM Universities WHERE Location IN (";
for(int i = 0; i < sz; ++i)
{
queryString += "?,";
}
queryString.back() = ')';
QSqlQuery query(queryString, db);
for(int i = 0; i < sz; ++i)
{
query.bindValue(i, ui->tableWidget->item(i, 0)->text());
}
if (!query.exec())
{
qDebug() << query.lastError().text();
// std::exit(1);
}
while(query.next())
{
qDebug() << query.value(0).toInt() << " "
<< query.value(1).toString() << " "
<< query.value(2).toInt() << "\n";
}
QSqlQuery query;
query.prepare("SELECT * FROM Universities WHERE Location IN (?)");
int sz = ui->tableWidget->rowCount();
QVariantList values;
for(int i = 0; i < sz; ++i)
{
values << ui->tableWidget->item(i, 0)->text();
}
query.addBindValue(values);
if (!query.execBatch())
{
qDebug() << query.lastError();
}
QStandardItemModel *model = new QStandardItemModel();
for (auto& x : tx->AddrsTo)
{
Qstring s(x.c_str());
QStringList items = s.split(';');
int sz = items.size();
for(int i = 0; i < sz; ++i)
{
model->setItem(0, i, new QStandardItem(items[i]));
}
}
this->findChild<QTableView*>("Adresses_tableView")->setModel(model);
QBuffer::QBuffer(QByteArray *byteArray, QObject *parent = nullptr)
QByteArray ba;
int n = 63;
ba.setNum(n); // ba == "63"
QBuffer buffer(&ba);
#include <QObject>
#include <QVector>
class Foo: public QObject
{
Q_OBJECT
Q_PROPERTY(QVector<bool> b READ getData WRITE setData)
public:
Foo()
{
m_b.resize(30);
for(auto &b:m_b)
{
b = true;
}
}
QVector<bool> getData()
{
return m_b;
}
void inv()
{
for(auto &b:m_b)
{
b = !b;
}
}
void setData(QVector<bool> b)
{
m_b = b;
}
private:
QVector<bool> m_b;
};
class Bar: public QObject
{
Q_OBJECT
Q_PROPERTY(QVector<bool> b WRITE setData)
public:
Bar(){};
void printData()
{
for(auto &b:m_b)
{
qDebug() << b << ' ';
}
}
void setData(QVector<bool> b)
{
m_b = b;
}
private:
QVector<bool> m_b;
};
int main(int argc, char *argv[])
{
Foo foo;
Bar bar;
bar.setData(foo.getData());
bar.printData();
qDebug() << "modify\n";
foo.inv();
bar.setProperty("b", foo.property("b"));
bar.printData();
}