PDA

View Full Version : why I cannot use QString?



Ryan111
1st April 2015, 14:45
Hi
I have written this program:


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
QString R,G,B;
int r,g,b,w;
char *ptr;
QString hex = ui->lineEdit->text();
R[0] = hex[0];
R[1] = hex[1];
G[0] = hex[2];
G[1] = hex[3];
B[0] = hex[4];
B[1] = hex[5];

r = (int)strtol( R , &ptr , 16 );
g = (int)strtol( G , &ptr , 16 );
b = (int)strtol( B , &ptr , 16 );
r = ( r & 0xF8);
g = ( g & 0xFC);
b = ( b & 0xF8);
w = r;
w = w<<6;
w = g;
w = w<<5;
w = b;

ui->lineEdit->setText(QString::number( w, 16 ));



}

this program is supposed to get a 24bit hex RGB and convert it to a 16bit hex RGB. this is 24bit:

http://www.willamette.edu/~gorr/classes/GeneralGraphics/imageFormats/24bits.gif

and this is 16bit:

http://upload.wikimedia.org/wikipedia/commons/f/f2/SLNotation56500.png

then we have to remove 3bit of red and blue color and 2bit of green color.

the problem is that strtol function cannot convert QString to const char. actually I get this error:

http://upload.tehran98.com/upme/uploads/f078a5e93ec0099e1.png

How can I figure out this problem?

yeye_olive
1st April 2015, 15:08
Do not use low-level C conversion functions to work on QString. QString has its own methods, such as QString::toUInt().

Besides, allocating a new QString object for each of the three parts of the original QString is a waste of resources. You could use QString::midRef() to get lightweight references to each interesting part of the QString, then call QStringRef::toUInt() to parse just that part. Alternatively, the format is so simple that you can write a custom parser by hand in a few lines of code.

Radek
1st April 2015, 17:06
bool ok = false;
uint hex = ui->lineEdit->text().toUInt(&ok,16);

if( ok )
{
// hex contains a binary value of the lineEdit
// supposing that the lineEdit contained a RGBA data, then
//
// alpha = (hex & 0xFF000000) >> 24;
// red = (hex & 0xFF0000) >> 16;
// green = (hex & 0xFF00) >> 8;
// blue = (hex & 0xFF);
//
// etc.
}
else
{
// lineEdit did not contain a binary value (perhaps, it was empty)
//
// report error or ignore
}

Ryan111
1st April 2015, 18:24
Thanks yeye_olive and Radek
I wrote a program but it just return 0
I gave it "F1D9B7" and in debug:

http://upload.tehran98.com/upme/uploads/c2c93513b60741971.png

if you consider, looks like midref() doesn't work correctly. why?:(

jefftee
1st April 2015, 19:09
Re-read the documentation for QString::midRef. The second argument is the number of characters, not the position. Based on that, shouldn't your code look like:



QStringRef R = hex.midRef(0,2);
QStringRef G = hex.midRef(2,2);
QStringRef B = hex.midRef(4,2);

yeye_olive
1st April 2015, 19:09
midRef() works just fine, but the values you pass for its parameter "n" are incorrect. You should re-read the documentation. In addition, some assignments to w should be |=.

stampede
1st April 2015, 19:11
QStringRef QString::midRef(int position, int n=-1)

Returns a substring reference to n characters of this string, starting at the specified position.

This function works fine, for your string:


hex = "F1D9B7"
R = midref(0,1) = <start at 0, get 1 character> = "F"
G = midref(2,3) = <start at 2, get 3 characters> = "D9B"
B = midref(4) = <start at 4, get all remaining characters> = "B7"

You can see the correct values in the debugger.
What were you expecting to get ?

Radek
1st April 2015, 19:16
It does, see jefftee (he was faster :) ), but the QStringRefs are unnecessary. Transform to binary the whole number first, then process the binary value. It is simpler and faster.

Ryan111
2nd April 2015, 07:40
Thanks guys!
I solved the problems and now it works like a watch!:cool:
but I think I must take a look in whole of QString class! it has some nice functions and other stuff to do your job quickly and easily! I love you Qt!:D