I am reading a QByteArray from binary file and need to convert it to a QVector<quint32>. I have tried the following code:
QVector<quint32> qByteArrayToQuint32Vector(const QByteArray bytes, QDataStream::ByteOrder byteOrder) {
QByteArray temp;
QVector<quint32> convertedData;
QDataStream stream(&temp, QIODevice::ReadWrite);
for (int i = 0; i < bytes.length(); i++) {
stream << bytes[i];
}
stream.setByteOrder(byteOrder);
quint32 t = 0;
for (int i = 0; i < bytes.length(); i += 4) {
stream >> t;
convertedData.push_back(t);
}
return convertedData;
}
It does not work, and the converted data is just zeroes.
It appears I've misunderstood the use of QDataStream. The working solution is below:
QVector<quint32> qByteArrayToQuint32Vector(const QByteArray bytes, QDataStream::ByteOrder byteOrder) {
int byteArraySize = bytes.length();
int vectorLen = byteArraySize/4;
QVector<quint32> convertedData;
convertedData.resize(vectorLen);
QDataStream in(bytes);
in.setByteOrder(byteOrder);
for (int i = 0; i < vectorLen; i++) {
in >> convertedData[i];
}
return convertedData;
}
Related
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue> // std::priority_queue
using std::vector;
using std::cin;
using std::cout;
struct fj{
int indexI=0;
int freeT=0;
};
struct DereferenceCompareNode : public std::binary_function<fj, fj, bool>
{
bool operator()(const fj lhs, const fj rhs) const
{
return lhs.freeT > rhs.freeT;
}
};
class JobQueue {
private:
int num_workers_;
vector<int> jobs_;
vector<int> assigned_workers_;
vector<long long> start_times_;
void WriteResponse() const {
for (int i = 0; i < jobs_.size(); ++i) {
cout << assigned_workers_[i] << " " << start_times_[i] << "\n";
}
}
void ReadData() {
int m;
cin >> num_workers_ >> m;
jobs_.resize(m);
std::cout<<"Read fault"<<"\n";
for(int i = 0; i < m; i++)
cin >> jobs_[i];
std::cout<<"Read fault ends"<<"\n";
}
void AssignJobs() {
// TODO: replace this code with a faster algorithm.
std::cout<<"Fault point 1"<<"\n";
assigned_workers_.resize(jobs_.size());
start_times_.resize(jobs_.size());
vector<long long> next_free_time(num_workers_, 0);
std::priority_queue<int, vector<int>, std::greater<int> > thread;
std::priority_queue<fj, vector<fj>, DereferenceCompareNode > freeJob;
/*
for (int i = 0; i < jobs_.size(); ++i) {
int duration = jobs_[i];
int next_worker = 0;
for (int j = 0; j < num_workers_; ++j) {
if (next_free_time[j] < next_free_time[next_worker])
next_worker = j;
}
assigned_workers_[i] = next_worker;
start_times_[i] = next_free_time[next_worker];
next_free_time[next_worker] += duration;
}
*/
std::cout<<"dump point 2"<<"\n";
for(int i=0;i<num_workers_;i++){
thread.push(i);
}
std::cout<<"dump point 1"<<"\n";
int counter = 0;
while(jobs_.size()!=0){
std::cout<<"jobs_.size:"<<jobs_.size()<<"\n";
std::cout<<"freeJob.size:"<<freeJob.size()<<"\n";
//check logic
do{
if(freeJob.top().freeT == counter){
std::cout<<"freeJob.top().freeT:"<<freeJob.top().freeT<<"\n";
std::cout<<"counter:"<<counter<<"\n";
thread.push(freeJob.top().indexI);
freeJob.pop();
}else{
break;
}
}
while(freeJob.size()!=0);
std::cout<<"Thread:"<<thread.size()<<"\n";
while(thread.size()!=0){
if(jobs_.size()!=0){
fj currA;
currA.indexI = thread.top();
currA.freeT = jobs_.at(0)+counter;
std::cout<<"currA.indexI:"<<currA.indexI<<"\n";
std::cout<<"currA.freeT:"<<currA.freeT<<"\n";
thread.pop();
jobs_.erase(jobs_.begin());
assigned_workers_.push_back(currA.indexI);
start_times_.push_back(currA.freeT);
}else{
break;
}
}
counter++;
}
}
public:
void Solve() {
ReadData();
AssignJobs();
WriteResponse();
}
};
int main() {
std::ios_base::sync_with_stdio(false);
JobQueue job_queue;
job_queue.Solve();
return 0;
}
I am getting segmentation fault in function ReadData while taking inputs for vector jobs.
I am getting fault even when I am inside bounds of defined size.
Everything was fine when have not written AssignJob function.
Am I doing something wrong with some bounds or taking illegal inputs format or messing with some other stuff?
Am I doing something wrong
Yes, you are: freeJob starts out empty, so this is undefined behavior:
if(freeJob.top().freeT == counter){
In fact, you never push anything into freeJob, you only pop() things from it.
I am storing some data in QDataStream and immediately taking the data, but the count is showing zero while retriving. code looks fine but unexpected behaviour
//Overloading
QDataStream& operator<< (QDataStream& writeTO, const CascadeJobInfo& data)
{
writeTO << data.m_infoJobType << data.m_connectionName << data.m_submitJobId << data.m_submitJobStat;
return writeTO;
}
QDataStream& operator>> (QDataStream& readIn, CascadeJobInfo& data)
{
readIn >> data.m_infoJobType >> data.m_connectionName >> data.m_submitJobId >> data.m_submitJobStat;
return readIn;
}
void Fun()
{
// Code Starts here
projectFileName = /*Path to folder*/
QFile file(projectFileName);
file.open(QFile::ReadWrite);
file.close();
QDataStream dStream(&file);
int jobLstCount = /*Get the Count, assume 4*/
dStream << jobLstCount;
for(int i = 0; i < jobLstCount; i++)
{
JobInfo.m_infoJobType = jobFlowItem->getJobType();
JobInfo.m_connectionName = submitItem->connectionName();
JobInfo.m_submitJobId = submitItem->jobID();
JobInfo.m_submitJobStat = submitItem->jobState();
// All valid data stored here
}
file.close();
QDataStream dStreamOut(&file);
dStreamOut >> jobLstCount; /*Count returns zero here insted of 4*/
CascadeJobInfo jobInfo;
// Why jobLstCount is getting zero here
for(int i = 0 ; i < jobLstCount ; i++)
{
dStreamOut >> jobInfo;
}
}
file.open(QFile::ReadWrite);
file.close(); <--- HERE
QDataStream dStream(&file);
You are closing the file as soon as you open it, so basically you are working with an invalid file descriptor which won't work. Put file.close() at the end of the code when you are done.
I am trying to implement a persistence in a Qtwebkit based browser for section keeping. I have extended QNetworkCookieJar. I didn't override any file but only added a save and load function. The functions is called explisitly.
I am getting the following error. The code is taken from brower code in Qt5.1 example.
QVariant::save: unable to save type 'QList<QNetworkCookie>' (type id: 1038).
Am I doing any thing wrong.. Please Help...Attaching my implementation of QNetworkCookie is bellow
static const unsigned int JAR_VERSION = 23;
#if 1
QT_BEGIN_NAMESPACE
QDataStream &operator<<(QDataStream &stream, const QList<QNetworkCookie> &list)
{
stream << JAR_VERSION;
stream << quint32(list.size());
for (int i = 0; i < list.size(); ++i)
stream << list.at(i).toRawForm();
return stream;
}
QDataStream &operator>>(QDataStream &stream, QList<QNetworkCookie> &list)
{
list.clear();
quint32 version;
stream >> version;
if (version != JAR_VERSION)
return stream;
quint32 count;
stream >> count;
for(quint32 i = 0; i < count; ++i)
{
QByteArray value;
stream >> value;
QList<QNetworkCookie> newCookies = QNetworkCookie::parseCookies(value);
if (newCookies.count() == 0 && value.length() != 0) {
qWarning() << "CookieJar: Unable to parse saved cookie:" << value;
}
for (int j = 0; j < newCookies.count(); ++j)
list.append(newCookies.at(j));
if (stream.atEnd())
break;
}
return stream;
}
QT_END_NAMESPACE
#endif
CookieJar::CookieJar(QObject *parent)
: QNetworkCookieJar(parent)
{
load();
}
CookieJar::~CookieJar()
{
save();
}
void CookieJar::load()
{
QSettings cookieSettings("./cookies_dir/Browser/cookies.ini", QSettings::IniFormat);
setAllCookies(qvariant_cast<QList<QNetworkCookie> >(cookieSettings.value(QLatin1String("cookies"))));
}
void CookieJar::save()
{
// purgeOldCookies();
QString directory = "./cookies_dir/Browser";
if (!QFile::exists(directory)) {
QDir dir;
dir.mkpath(directory);
}
QSettings cookieSettings(directory + QLatin1String("/cookies.ini"), QSettings::IniFormat);
QList<QNetworkCookie> cookies = allCookies();
qWarning("\n\n+=======================================================+\n\n");
for (int i = cookies.count() - 1; i >= 0; --i) {
qWarning()<< cookies.at(i).domain();
qWarning()<< cookies.at(i).name();
if (cookies.at(i).isSessionCookie())
cookies.removeAt(i);
}
qWarning("\n\n+=======================================================+\n\n");
cookieSettings.setValue(QLatin1String("cookies"), QVariant::fromValue<QList<QNetworkCookie> >(cookies));
}
void CookieJar::purgeOldCookies()
{
QList<QNetworkCookie> cookies = allCookies();
if (cookies.isEmpty())
return;
int oldCount = cookies.count();
QDateTime now = QDateTime::currentDateTime();
for (int i = cookies.count() - 1; i >= 0; --i) {
if (!cookies.at(i).isSessionCookie() && cookies.at(i).expirationDate() < now)
cookies.removeAt(i);
}
if (oldCount == cookies.count())
return;
setAllCookies(cookies);
}
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
{
qWarning()<< url;
return QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
}
I have registered the CookieJar using bellow code.
jar = new CookieJar(this);
webView->page()->networkAccessManager()->setCookieJar(jar);
You must call qRegisterMetaTypeStreamOperators<QList<QNetworkCookie>>("QList<QNetworkCookie>") before saving or loading to QSettings.
You need to use qRegisterMetaTypeStreamOperators and defines stream operators.
Here is an example save QList< QNetworkCookie> type cookies. In the header declares stream operators :
class cookieJar: public QNetworkCookieJar
{
bool save(const QUrl& url);
bool load(const QUrl& url);
}
QDataStream &operator<<(QDataStream &out, const QNetworkCookie &cookie);
QDataStream &operator>>(QDataStream &in, QNetworkCookie &cookie);
In cpp file you do something like:
CookieJar::CookieJar(QObject* parent) :
QNetworkCookieJar(parent)
{
qRegisterMetaTypeStreamOperators<QNetworkCookie>("QNetworkCookie");
}
bool CookieJar::save(const QUrl &url)
{
QList<QNetworkCookie> cookies = cookiesForUrl(url);
QSettings cookieSettings;
for(auto cookie : cookies)
{
cookieSettings.setValue(cookie.name(), QVariant::fromValue(cookie));
}
}
bool CookieJar::load(const QUrl &url)
{
QSettings cookieSettings;
// empty list created here
QList<QNetworkCookie> cookies;
// retrieve all keys from cookies.ini
QStringList keys = cookieSettings.allKeys();
for(auto key: keys)
{
QNetworkCookie cookie = qvariant_cast<QNetworkCookie>(cookieSettings.value(key, QVariant()));
cookies.append(cookie);
}
setCookiesFromUrl(cookies, url);
return true;
}
QDataStream &operator<<(QDataStream &out, const QNetworkCookie &cookie)
{
out << cookie.toRawForm();
}
QDataStream &operator>>(QDataStream &in, QNetworkCookie &cookie)
{
QByteArray raw_data;
in >> raw_data;
QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(raw_data);
cookie = cookies.at(0);
}
I need to perform some regexp operations on binary data. I wrote a function to convert QByteArray data in a hexa string representation. Each byte is prepended by 'x' for parsing purpose.
How could this code be optimized?
QByteArray data;
QByteArray newData;
for (int i = 0; i < data.size(); i++) {
QString hex;
hex.setNum(data[i], 16);
if (data[i] < 10) {
hex.prepend("x0");
} else {
hex.prepend("x");
}
newData.append(hex.toLatin1());
}
The code you posted has two bugs in it that I corrected.
1) Assuming you always want two hex digits you want to check if the value is less than 16, not 10.
2) QString::setNum has no overload for char, so the value is promoted to a larger type. For a value like 128, which is negative in a signed char, you would get x0ffffffffffffff80 due to sign extension.
The function foo1 is your original code with the bugs fixed, and foo2 is a more optimal version that avoids creating a temporary QString since the conversion to unicode and back isn't free, and prepending values to a string requires additional copying.
I used QElapsedTimer because on Windows where I am testing it uses the high resolution PerformanceCounter clock. If you are on another platform it might be less accurate. You can see the different types of clocks it may use in the documentation.
Set display_converted_string to true if you want the converted string printed to verify they are identical.
#include <QString>
#include <QByteArray>
#include <QElapsedTimer>
#include <iostream>
QByteArray foo1(QByteArray data)
{
QByteArray newData;
for (int i = 0; i < data.size(); i++) {
unsigned char c = data[i];
QString hex;
hex.setNum(c, 16);
if (c < 16) {
hex.prepend("x0");
} else {
hex.prepend("x");
}
newData.append(hex.toLatin1());
}
return newData;
}
QByteArray foo2(QByteArray data)
{
static const char digits[] = {'0','1','2','3','4','5','6','7',
'8','9','a','b','c','d','e','f'};
QByteArray newData;
newData.reserve(data.size() * 3);
for (int i = 0; i < data.size(); i++)
{
unsigned char c = data[i];
newData.append('x');
newData.append(digits[(c >> 4) & 0x0f]);
newData.append(digits[c & 0x0f]);
}
return newData;
}
int main()
{
const int iterations = 10000;
const bool display_converted_string = false;
QElapsedTimer t;
std::cout << "Using clock type " << t.clockType() << ".\n";
QByteArray data(256, 0);
QByteArray newData;
qint64 elapsed1 = 0, elapsed2 = 0;
//Set the values in data to 0-255 to make sure all values are converted properly.
for(int i = 0; i < data.size(); ++i)
{
data[i] = i;
}
t.start();
for(int i = 0; i < iterations; ++i)
{
newData = foo1(data);
}
elapsed1 = t.nsecsElapsed();
std::cout << "foo1 elapsed time = " << elapsed1 << "\n";
if(display_converted_string)
{
std::cout << "newData = " << newData.data() << "\n";
}
t.restart();
for(int i = 0; i < iterations; ++i)
{
newData = foo2(data);
}
elapsed2 = t.nsecsElapsed();
std::cout << "foo2 elapsed time = " << elapsed2 << "\n";
if(display_converted_string)
{
std::cout << "newData = " << newData.data() << "\n";
}
return 0;
}
How can I read and write to a text file date enter to a QTableView?
This is what I have but I would like to save the data when it is added to the table and of course be able to read it back when the application is reopened. Is there any tutorial I can refer to?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QStandardItemModel();
model->setRowCount(0);
ui->tableView->setModel(model);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QStandardItem *userName = new QStandardItem(ui->lineEdit_Name->text());
QStandardItem *userNumber = new QStandardItem(ui->lineEdit_Number->text());
QList<QStandardItem*> row;
row <<userName << userNumber;
model->appendRow(row);
}
Thanks a lot
EDIT --------------------------------
This is what worked for me:
Add Function:
void MainWindow::on_pushButto_Add_clicked() {
QStandardItem *userInput = new QStandardItem(ui->lineEdit->text());
QStandardItem *userInput2= new QStandardItem(ui->lineEdit_2->text());
QList<QStandardItem*> row;
row <<userInput << userInput2;
model->appendRow(row);
}
Save Function:
void MainWindow::on_pushButton_Save_clicked()
{
QFile file("C:/Users/UserName/Practicing/Resources_Files/someFile.bin");
if (file.open(QIODevice::WriteOnly))
{
QDataStream stream(&file);
qint32 n = model->rowCount();
qint32 m = model->columnCount();
stream << n << m;
for (int i=0; i<n; ++i)
{
for (int j=0; j<m; j++)
{
model->item(i,j)->write(stream);
}
}
file.close();
}
}
Load Function:
void MainWindow::on_pushButton_Load_clicked()
{
QFile file("C:/Users/UserName/Practicing/Resources_Files/someFile.bin");
if (file.open(QIODevice::ReadOnly))
{
QDataStream stream(&file);
qint32 n, m;
stream >> n >> m;
model->setRowCount(n);
model->setColumnCount(m);
for (int i = 0; i < n ; ++i) {
for (int j = 0; j < m; j++) {
QStandardItem *item = new QStandardItem;
item->read(stream);
model->setItem(i, j, item);
}
}
file.close();
}
}
QFile file("somefile.bin");
if (file.open(QIODevice::WriteOnly)) {
QDataStream stream(&file);
stream << *(model->invisibleRootItem());
file.close();
}
http://qt-project.org/doc/qt-5.0/qtgui/qstandarditemmodel.html#invisibleRootItem
Edit:
Here is correction (I've checked that it works).
void MainWindow::save()
{
QFile file("somefile.bin");
if (file.open(QIODevice::WriteOnly)) {
QDataStream stream(&file);
qint32 n(model->rowCount()), m(model->columnCount());
stream << n << m;
for (int i=0; i<n; ++i)
for (int j=0; j<m; j++)
model->item(i,j)->write(stream);
file.close();
}
}
void MainWindow::load()
{
QFile file("somefile.bin");
if (file.open(QIODevice::ReadOnly)) {
QDataStream stream(&file);
qint32 n, m;
stream >> n >> m;
model->setRowCount(n);
model->setColumnCount(m);
for (int i=0; i<n; ++i)
for (int j=0; j<m; j++)
model->item(i,j)->read(stream);
file.close();
}
}
You can browse your model row by row, column by column and fill a file with a format like CSV (a row by line and columns separated by coma or tabs).
But, I don't think that is a good idea to modify the file when an item has changed. You should write the file when your application is closed.
model->item(i,j)->write(stream); will lead to segmentation fault if item(i,j) is empty. Assign some dummy value like whitespace in empty cells.