when I write the image path directly like this
QPixmap imgBtry("/some/path/resources/Battery100.png");
it works perfectly but its not working when i store the path in a variable. What should i do? Following is the complete code.
//global variables
std::string path;
std::string img100 = "/some/path/resources/Battery100.png";
std::string img75 = "/some/path/resources/Battery75.png";
std::string img50 = "/some/path/resources/Battery50.png";
std::string img25 = "/some/path/resources/Battery25.png";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap imgBtry(img50);
ui->label_2->setPixmap(imgBtry.scaled(50,50,Qt::IgnoreAspectRatio,Qt::FastTransformation));
}
What error do you get? A guess could be:
The QPixmap constructor takes a QString as argument. It works when you put the string directly because it is a c-string (char *) and QString have an constructor taking an c-string as input. But have no constructor taking a std::string as input.
So either:
1) define your strings as c-strings.
or
2) convert your std::strings to c-strings before calling QPixmap:
QPixmap imgBtry(img50.c_str());
Related
I have a program that is using HeapAlloc() to allocate a struct that contains a QString and a few QByteArray structs.
Struct code:
struct Environment {
QString internalLinksDomain;
QByteArray aboutTelegram;
QByteArray aboutContacts;
QByteArray aboutFrequent;
QByteArray aboutSessions;
QByteArray aboutWebSessions;
QByteArray aboutChats;
QByteArray aboutLeftChats;
};
My code:
Environment* e = (Environment*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Environment));
e->internalLinksDomain = "Test data";
I have also tried..
e->internalLinksDomain.append("Test data");
Both send me to the atomic file in Visual Studio. I have attached a screen shot of the error and its position in the function causing the error.
VS Error
I am trying to add a QPixmap to a QLabel taken from another QLabel but there is an error :
Here is the code
const QPixmap *tempPix = new QPixmap("");
tempPix = (label1->pixmap());
label2->setPixmap(tempPix); //error cannot convert from const QPixmap* to const QPixmap&
and if I do it like this:
const QPixmap tempPix("");
tempPix = (label1->pixmap()); //error cannot convert QPixmap and QPixmap*
label2->setPixmap(tempPix);
To copy data from a pointer object to an object you must use the *
QPixmap tempPix;
if(label1->pixmap()){
tempPix = *label1->pixmap();
label2->setPixmap(tempPix);
}
You can write it in a single line as follows:
label2->setPixmap(*label1->pixmap());
Note that * will convert the pointer returned by pixmap() to a reference. The difference between both is explained in this thread.
Note that in your first example, the constructed QPixmap in the first line is never used and a memory leak occurs. The second line changes the pointer value, not the data of the newly constructed object.
If I put url http://www.äsdf.de/bla/bla into QUrl, how can I then restore url with original symbols?
It's ok that QUrl will fix some characters, but I'd like to display original äsdf in url instead of xn--sdf-pla.
I am aware about QString QUrl::fromAce(const QByteArray &domain), but it requires QByteArray instead of QUrl instance.
You can use QUrl::toDisplayString with the default PrettyDecoded formatting option, or any other value among enum ComponentFormattingOption:
QUrl url{"http://www.äsdf.de/bla/bla"};
QString original_string =
url.toDisplayString(); // default is QUrl::PrettyDecoded
QString original_string_with_encoded_spaces =
url.toDisplayString(QUrl::EncodeSpaces);
In the following code the method exec(const char *cmd) runs a bash script and returns the output as a vector of strings. The intent is to create a QListWidgetItem out of each of those strings and add them to a QListWidget on a little GUI that I have created, but the QListWidgetItems are not created successfully. I always seem to be required to use a const char* to create either a QString or QListWidgetItem, it will not allow me to create one using a string variable.
You can see what I am going for in the line: QString nextLine = txtVcr.back();
There is an exception thrown here, it wants QString set to a const char*, for example QString nextLine = "Hello, World!";
How do I go about getting the strings from my vector and creating QListWidgetItems out of them to add to my QListWidget?
In C# everything was rather direct in that I could add strings or whatever else to any container/widget. Is there an intermediate step that I am overlooking with these "QWidgets"? Perhaps I should be casting to "Q" types?
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
vector<string> exec(const char *cmd);
vector<string> txtVcr = exec("/home/rhurac/getServices.sh");
while (!txtVcr.empty())
{
QString nextLine = txtVcr.back();
ui->uxListWidget->addItem(new QListWidgetItem(nextLine, ui->uxListWidget));
txtVcr.pop_back();
}
}
To get a QStringList from a std::vector<std::string>, you'll need to use QString::fromStdString on all the elements. For example:
#include <QStringList>
#include <algorithm>
#include <string>
#include <vector>
QStringList convert(const std::vector<std::string>& v)
{
using std::begin;
using std::end;
QStringList l;
l.reserve(v.size());
std::transform(begin(v), end(v), std::back_inserter(l),
&QString::fromStdString);
return l;
}
Then you can populate a QStringListModel feeding a QListView, as suggested in other answer.
Simply don't use QListWidgets and other QxyzWidget classes. They are depricated, and left in Qt for compatibility with old code (Qt3 basically).
Use QListView and QStringListModel for your use-case. E.g.
QListView *lv = new QListView();
QStringListModel m;
QStringList data = QStringList()<<"AAA"<<"BBB"<<"CCC";
m.setStringList(data);
lv->setModel(&m);
lv->show();
P.S.: Sorry, it doesn't answer your question directly. But unless you have to support legacy code, don't touch QListWidgets!
I am trying to learn how to use JSON and the Qt JSON classes. For example I wnat to create a simple QJsonDocument, save it to a file, load it into a different QJsonDocument and compare results.
I managed to create a QJsonDocument. However there is no simple command in the QJsonDocument interface to save it to a file. The same goes for loading the document from a file.
#include <QJsonObject>
#include <QJsonDocument>
#include <QVariant>
int main()
{
QVariantMap map;
map.insert("integer", 1);
map.insert("double", 2.34);
map.insert("bool", QVariant(true));
map.insert("string", "word");
QJsonObject object = QJsonObject::fromVariantMap(map);
QJsonDocument document;
document.setObject(object);
// ?? save document to file
// ?? load file to document
return 0;
}
This answer shows how to load the document by
reading to a QFile
converting QFile to a QString
converting the QString to a QByteArray
constructing the QJsonDocument from the QByteArray
Is there a more straightforward way to do this?
Personally, I think that code [that you linked to] looks a bit messy. Warning: head compiled code follows.
QJsonDocument loadJson(QString fileName) {
QFile jsonFile(fileName);
jsonFile.open(QFile::ReadOnly);
return QJsonDocument().fromJson(jsonFile.readAll());
}
void saveJson(QJsonDocument document, QString fileName) {
QFile jsonFile(fileName);
jsonFile.open(QFile::WriteOnly);
jsonFile.write(document.toJson());
}
This may not be perfect: it assumes QFile instead of QIODevice, but if you're dealing with only local files maybe it won't matter. You can then use these functions instead of repeating the Json load/save code everytime you need to load/save Json.
No need for converting to string and back. With QSettings and QVariant classes you can easily do that. Create QVariant object from QJsonDocument and save it with QSettings. Look at functions QJsonDocument::fromVariant and QJsonDocument::toVariant. Combine them with QSettings class and specifically void QSettings::setValue ( const QString & key, const QVariant & value ) method, that works well with QVariant and that's it.
Also QSettings class has this constructor QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 )
that would allow you to set path to the file - fileName variable