How to include .txt on Qt
-
From what you wrote before, ci.ImageName is empty, correct ?
If so, check the key that you use to retrieve the data from the map. If not part of the map, depending on the technique used, you will get a default constructed object.
-
Yes it's empty.
foreach( QRgb key, Costs.keys() ) { QColor BaseColor( key ); if (Costs.contains( colour.rgb() ) || IsCloseColor(BaseColor, colour) ) { CostInfo& ci = Costs[colour.rgb()]; QLabel abc; // int Cost = ci.Cost; qDebug() << "check me--->" << ci.ImageName; QPixmap pix( ci.ImageName ); if (pix.isNull()){ qDebug() << "yep its no good"; return; }; QPixmap scaledPix = pix.scaled( 8, 8, Qt::KeepAspectRatio, Qt::SmoothTransformation ); abc.setPixmap(scaledPix); abc.show();
it's what i have.
-
Your logic is flawed. If Costs contains
colour.rgb()
then you can go on with the rest of your logic else you should go through your map and useIsCloseColor
to determine which key is good. Then you can use that key to go further. -
You're already doing it with your foreach loop and the documentation of QMap shows several ways of looping through its content.
-
something like that ?
foreach( QRgb key, Costs.keys() ) { QColor BaseColor( key ); if (Costs.contains( colour.rgb() )) { CostInfo& ci = Costs[colour.rgb()]; QLabel abc; // int Cost = ci.Cost; qDebug() << "check me--->" << ci.ImageName; QPixmap pix( ci.ImageName ); if (pix.isNull()){ qDebug() << "yep its no good"; return; }; QPixmap scaledPix = pix.scaled( 8, 8, Qt::KeepAspectRatio, Qt::SmoothTransformation ); abc.setPixmap(scaledPix); abc.show(); } else { if (IsCloseColor(BaseColor, colour) == true){ CostInfo& ci = Costs[colour.rgb()]; QColor BaseColor( key ); QPixmap pix( ci.ImageName ); QPixmap scaledPix = pix.scaled( 8, 8, Qt::KeepAspectRatio, Qt::SmoothTransformation ); abc.setPixmap(scaledPix); abc.show(); } else { qDebug() << "yep its no good"; } }
-
Move the foreach loop in the else part. There's no sense in looping through the whole map if it contains
colour.rgb()
. Also note that you won't see any QLabel since it's going out of scope. -
void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectangle, const QColor& colour) { int maxX = topLeft.x() + rectangle.width(); int maxY = topLeft.y() + rectangle.height(); for(int x = topLeft.x(); x < maxX; ++x) { for(int y = topLeft.y(); y < maxY; ++y) { image.setPixelColor(x, y, colour); QColor BaseColor( key ); if (Costs.contains( colour.rgb() )) { CostInfo& ci = Costs[colour.rgb()]; QLabel abc; // int Cost = ci.Cost; qDebug() << "check me--->" << ci.ImageName; QPixmap pix( ci.ImageName ); if (pix.isNull()){ qDebug() << "yep its no good"; return; }; QPixmap scaledPix = pix.scaled( 8, 8, Qt::KeepAspectRatio, Qt::SmoothTransformation ); abc.setPixmap(scaledPix); abc.show(); } else { if (IsCloseColor(BaseColor, colour) == true){ foreach( QRgb key, Costs.keys() ) { CostInfo& ci = Costs[colour.rgb()]; QLabel abc; QColor BaseColor( key ); QPixmap pix( ci.ImageName ); QPixmap scaledPix = pix.scaled( 8, 8, Qt::KeepAspectRatio, Qt::SmoothTransformation ); abc.setPixmap(scaledPix); abc.show(); } else { qDebug() << "yep its no good"; } }
i changed what u said
-
I'd guess it doesn't compile.
Your BaseColor variable doesn't make sense where it's declared. The
if (IsCloseColor...
also doesn't make sense where you put it. You are searching the map to find which of its key is close to the colour you received. But currently if BaseColor fits you are just trying to show each and every element ofCosts
.Again: your QLable objects will be destroyed before they are even shown.
-
Yes but i dont want to display one picture so the label is here just for display the picture from my QMap my final picture with all the QMap's image is display in the label_3
sorry but i loose you ^^
the if (IsClose .. doesnt make sense ? i put it after the else because if the bool dont return true we can't continue no?
""BaseColor fits you are just trying to show each and every element of Costs.""
so maybe replace "foreach( QRgb key, Costs.keys() )" by just "foreach(only the image)" can i write ImageName.key ? -
@Payx
I think what @SGaist says is thatif
Costs
containscolour.rgb()
you can do what you want
else you should go though theCosts
to search if there is aBaseColor
that is close tocolour
and if there is one, you can do what you wantwhat you do now is
ifCosts
containscolour.rgb()
you do what you want
else test ifBaseColor
is close tocolour
if it is then you go though theCosts
and do what you wantthe difference is that
you are not search for aBaseColor
that is close tocolour
( which is aforeach
thenif
)
but test ifBaseColor
is close tocolour
and go though theCosts
( which is aif
thenforeach
)
and theBaseColor
is unknown since there is nokey
defined before it -
No, what you are doing is that if colour is close to base colour you loop on your map.
What you want to do is loop on the map and if any of the colour ĆsCloseColour` then use that one.
As for the build error, look where you declared key and where you are currently using it.
-
@SGaist said in How to include .txt on Qt:
What you want to do is loop on the map and if any of the colour ĆsCloseColour` then use that one.
Sorry if i don't understand well but """IsCloseColor(BaseColor, colour) == true""" dont mean that if any of the color IsCloreColour ?
-
No, that function checks the whether the colours passed as parameters are close.
What you want to do is check which one of the map entries is close to the reference.
Take a look at your code and the order in which you wrote it. You are not testing the content of the Costs map, you are just checking whether
colour
is close to BaseColor which currently you can't create since your key variable is inside your loop and not outside where you declared BaseColor. -
-
Looks better, yes.
Don't forget to break out of the loop once you found a match.
-
so it compile but i got the 3 same errors :
QPixmap::scaled: Pixmap is a null pixmap setGeometry: Unable to set geometry 5x13+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry: 120x13+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215). yep its no good
void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectangle, const QColor& colour) { int maxX = topLeft.x() + rectangle.width(); int maxY = topLeft.y() + rectangle.height(); for(int x = topLeft.x(); x < maxX; ++x) { for(int y = topLeft.y(); y < maxY; ++y) { image.setPixelColor(x, y, colour); if (Costs.contains( colour.rgb() )) { CostInfo& ci = Costs[colour.rgb()]; QLabel abc; // int Cost = ci.Cost; qDebug() << "check me--->" << ci.ImageName; QPixmap pix( ci.ImageName ); if (pix.isNull()){ qDebug() << "yep its no good"; return; }; QPixmap scaledPix = pix.scaled( 8, 8, Qt::KeepAspectRatio, Qt::SmoothTransformation ); abc.setPixmap(scaledPix); abc.show(); } else { foreach( QRgb key, Costs.keys() ) { QColor BaseColor( key ); if (IsCloseColor(BaseColor, colour) == true){ CostInfo& ci = Costs[colour.rgb()]; QLabel abc; QPixmap pix( ci.ImageName ); QPixmap scaledPix = pix.scaled( 8, 8, Qt::KeepAspectRatio, Qt::SmoothTransformation ); abc.setPixmap(scaledPix); abc.show(); } else { qDebug() << "yep its no good"; } } } } }}
i put my code here
-
Because you are still trying to get the wrong entry from your map. If you enter your foreach loop, it means that
colour
can't be found in your map thus you can't use it to get the value from the map.IsCloseColor
tells you thatBaseColor
andcolour
are close, then you must usekey
to get the CostInfo you want.