A problem i have with palettes i need help with!
-
So Pos is the end/start of the next gradient ?
-
Hi
You should be able to use such table with
https://doc.qt.io/qt-5/qgradient.html -
Hi
You should be able to use such table with
https://doc.qt.io/qt-5/qgradient.html -
@mrjj never worked with QPainter or QLinearGradient befor! but i'll give it a go i guess
Well fast example might help you get started.
// converts range between 0-255 to range 0 - 1 as gradient uses float mapPos(int x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } // data type to hold the info as list of ints is clumsy struct gradPosDef { int pos; int r; int g; int b; }; // define a test gradient. this you would load from a file. QList<gradPosDef> gradValues{ {0, 120, 0, 0}, {22, 179, 22, 0}, {51, 255, 104, 0}, {85, 167, 22, 18}, {135, 100, 0, 103}, {198, 16, 0, 130}, {255, 0, 0, 160} };
// creates pixmap and returns it QPixmap MainWindow::makeGradient() { // size of gradient / pixmap const int gw = 255; const int gh = 100; // define it- make it point straight from left to right QLinearGradient gradient(0, 0, gw, 0); // loop over the test gradient values for (int cc = 0; cc < gradValues.size(); cc++) { auto cur = gradValues.at(cc); // convert to new scale. you could directly use 0- 1 instead of 0-255 to avoid float scaledVal = mapPos(cur.pos, 0, 255, 0, 1); // write it out to see if we get sane values qDebug() << "org: " << cur.pos << " - " << scaledVal; // make color QColor col(cur.r, cur.g, cur.b); // set gradient point gradient.setColorAt(scaledVal, col); }; // allocate a pix map QPixmap pix(gw, gh); //assign painter to it so we can paint the gradiant QPainter p(&pix); // assign gradient to a brush. this way we can paint it QBrush grad(gradient); // fill pixmap with gradiant p.fillRect(pix.rect(), gradient); return pix; }
to use it you can do
auto pix = makeGradient(); pix.save("e:/test.png"); ui->label->setPixmap(pix);
and get
Not perfect but demonstrate one way to do it.
-
Well fast example might help you get started.
// converts range between 0-255 to range 0 - 1 as gradient uses float mapPos(int x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } // data type to hold the info as list of ints is clumsy struct gradPosDef { int pos; int r; int g; int b; }; // define a test gradient. this you would load from a file. QList<gradPosDef> gradValues{ {0, 120, 0, 0}, {22, 179, 22, 0}, {51, 255, 104, 0}, {85, 167, 22, 18}, {135, 100, 0, 103}, {198, 16, 0, 130}, {255, 0, 0, 160} };
// creates pixmap and returns it QPixmap MainWindow::makeGradient() { // size of gradient / pixmap const int gw = 255; const int gh = 100; // define it- make it point straight from left to right QLinearGradient gradient(0, 0, gw, 0); // loop over the test gradient values for (int cc = 0; cc < gradValues.size(); cc++) { auto cur = gradValues.at(cc); // convert to new scale. you could directly use 0- 1 instead of 0-255 to avoid float scaledVal = mapPos(cur.pos, 0, 255, 0, 1); // write it out to see if we get sane values qDebug() << "org: " << cur.pos << " - " << scaledVal; // make color QColor col(cur.r, cur.g, cur.b); // set gradient point gradient.setColorAt(scaledVal, col); }; // allocate a pix map QPixmap pix(gw, gh); //assign painter to it so we can paint the gradiant QPainter p(&pix); // assign gradient to a brush. this way we can paint it QBrush grad(gradient); // fill pixmap with gradiant p.fillRect(pix.rect(), gradient); return pix; }
to use it you can do
auto pix = makeGradient(); pix.save("e:/test.png"); ui->label->setPixmap(pix);
and get
Not perfect but demonstrate one way to do it.
-
@mrjj ty for that bit of code m8! :) <3
question! is this the way togo when you want to save the file to the App folder?
if ( pix.save(QDir::currentPath() + "/test.png") )
@Kris-Revi You should not write into app folder as usually normal users have no write access there!
There are better options for saving data, see https://doc.qt.io/qt-5/qstandardpaths.html -
@Kris-Revi You should not write into app folder as usually normal users have no write access there!
There are better options for saving data, see https://doc.qt.io/qt-5/qstandardpaths.html -
@Kris-Revi Did you actually read the link I gave you?
There are several locations depending on type of files:- QStandardPaths::DocumentsLocation - any documents
- QStandardPaths::PicturesLocation - for pictures (this is probably what you want)
- ...
-
You can either use QFileDialog so your users can choose where to store them or use one of the suitable proposition of QStandardPaths like
QStandardPaths::DocumentsLocation
-
@Kris-Revi Did you actually read the link I gave you?
There are several locations depending on type of files:- QStandardPaths::DocumentsLocation - any documents
- QStandardPaths::PicturesLocation - for pictures (this is probably what you want)
- ...
-
@jsulm ofcourse i read it! i was asking where do people usualy put it :) im not THAT lazy! :)
@Kris-Revi Sorry if I was rude!
Where to put the data depends on data types. It looks like you want to store a picture, so QStandardPaths::PicturesLocation would be the proper location. Or you ask the user where to store as @SGaist suggested. -
@Kris-Revi Sorry if I was rude!
Where to put the data depends on data types. It looks like you want to store a picture, so QStandardPaths::PicturesLocation would be the proper location. Or you ask the user where to store as @SGaist suggested.@jsulm np! was not rude :)
im asking because when installing almost any software it always creates a folder in the "C:/user/username/appdata" folder was wondering if that was a default/standard !
suggesting documents seemed weird to me as these "pictures" are generated and used by the APP and not the user and as i write this i think (appdata) would be the place
-
@jsulm np! was not rude :)
im asking because when installing almost any software it always creates a folder in the "C:/user/username/appdata" folder was wondering if that was a default/standard !
suggesting documents seemed weird to me as these "pictures" are generated and used by the APP and not the user and as i write this i think (appdata) would be the place
@Kris-Revi said in A problem i have with palettes i need help with!:
C:/user/username/appdata
Yes, this folder can be used by your app to write its data in its subfolder. But "pix.save(QDir::currentPath()" will not return this path. Use QStandardPaths::AppLocalDataLocation to get that path.
-
@Kris-Revi said in A problem i have with palettes i need help with!:
C:/user/username/appdata
Yes, this folder can be used by your app to write its data in its subfolder. But "pix.save(QDir::currentPath()" will not return this path. Use QStandardPaths::AppLocalDataLocation to get that path.
@jsulm said in A problem i have with palettes i need help with!:
Yes, this folder can be used by your app to write its data in its subfolder. But "pix.save(QDir::currentPath()" will not return this path. Use QStandardPaths::AppLocalDataLocation to get that path.
yea i wrote som vars
QString save_path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); QString palette_folder = "/palettes";
-
@Kris-Revi said in A problem i have with palettes i need help with!:
C:/user/username/appdata
Yes, this folder can be used by your app to write its data in its subfolder. But "pix.save(QDir::currentPath()" will not return this path. Use QStandardPaths::AppLocalDataLocation to get that path.
-
@jsulm hmm but for android there is no such path as
C:/user/username/appdata
.... where would i store them there?@Kris-Revi There should be app data folder (of course it is not C:/user/username/appdata on Android). Try QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) on Android and see what it returns.
-
QStandardPaths will return you suitable paths for the platform you are targeting however you should always check that the folder exists and create it if not prior to do anything else. While the path you will get is valid, it does not mean it exists yet as it does not make sense for the system to create them all since not every app will use these folders. Especially on mobile OS where the application storage and the user data it may create are located on different spaces on the device.
-
QStandardPaths will return you suitable paths for the platform you are targeting however you should always check that the folder exists and create it if not prior to do anything else. While the path you will get is valid, it does not mean it exists yet as it does not make sense for the system to create them all since not every app will use these folders. Especially on mobile OS where the application storage and the user data it may create are located on different spaces on the device.
@SGaist ofcourse :) i've made a init file that on startup checks every folder, path and file, if they exists (fi not create them) and if can we access them :) for this i made this
Debug_pff.LevelInfo("PFF", "Checking if folder exists", "!"); if ( !QDir(save_path + palette_folder).exists() ) { QDir().mkdir(save_path + palette_folder); Debug_pff.LevelInfo("PFF", "Folder did not exist so we created one", "!"); }