How to include .txt on Qt
-
QMapIterator<QRgb, CostInfo> i;
is wrongIt should be something like
QMap<QRgb, CostInfo>::const_iterator i = Costs.constBegin(); while (i.hasNext()) { //etc. }
-
Please, show the latest version of your code.
-
Mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPixmap> #include <QImage> #include <QFileDialog> #include <QColor> #include <QPoint> #include <QSize> #include <iostream> #include <QMapIterator> #include <QMap> using namespace std; QColor Couleurdominante(const QImage& image, const QPoint& topLeft, const QSize& rectSize); QMap<QRgb, CostInfo > Costs; void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour); bool IsCloseColor( QColor c1, QColor c2 ); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_push_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)")); QPixmap pix(fileName); ui->label->setPixmap(pix); const QSize s = pix.size(); pixi = QImage(pix.toImage()); ui->label_2->setText( "Size: " + QString::number(s.width()) +" "+ QString::number(s.height()) ); } void MainWindow::on_push2_clicked() { for (int i=0;i<pixi.width();i+=z){ for (int j=0;j<pixi.height();j+=z){ k=k+0.06; Remplissage(pixi,QPoint(i,j),QSize(z,z),Couleurdominante(pixi,QPoint(i,j),QSize(z,z))); } } pixa=QPixmap::fromImage(pixi); ui->label_3->setPixmap(pixa); float a = k - 0.06*z; ui->label_4->setText(QString::number(a)); } 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); QMap<QRgb, CostInfo>::const_iterator i = Costs.constBegin();; while (i.hasNext()) { i.next(); QColor BaseColor( i.key() ); if (Costs.contains( colour.rgb() ) || IsCloseColor(BaseColor, colour) ) { CostInfo& ci = Costs[colour.rgb()]; int Cost = ci.Cost; QPixmap pix( ci.ImageName ); } } } } } QColor Couleurdominante(const QImage & image,const QPoint & topLeft, const QSize & rectangle) { int rouge = 0, vert = 0, bleue = 0; int X = topLeft.x() + rectangle.width(); int Y = topLeft.y() + rectangle.height(); for (int y = topLeft.y(); y < Y; y++) { for (int x = topLeft.x(); x < X; x++) { QRgb pixel = image.pixel(x, y); rouge += qRed(pixel); vert += qGreen(pixel); bleue += qBlue(pixel); } } int n = rectangle.width() * rectangle.height(); Q_ASSERT(n); if (n <= 0) return Qt::black; return QColor(rouge / n, vert / n, bleue / n); } QMap<QRgb, CostInfo > Costs = { { QColor(255 , 0 , 0 ).rgb(), { "://fraise.png", 10 }}, { QColor(0 , 255 , 0 ).rgb(), { "://balleverte.png", 20 }}, { QColor(0 , 0 , 255 ).rgb(), { "://ballebleue.png", 20 }}, { QColor(255 , 255 , 255 ).rgb(), { "://balleblanche.png", 20 }}, { QColor(255 , 128 , 0 ).rgb(), { "://ballepeche.png", 20 }}, { QColor(0 , 0 , 0 ).rgb(), { "://noir.png", 20 }}, { QColor(102 , 51 , 0 ).rgb(), { "://marron.png", 20 }}, { QColor(255 , 102 , 78 ).rgb(), { "://rose.png", 20 }}, { QColor(0 , 204 , 204 ).rgb(), { "://turquoise.png", 20 }}, { QColor(255 , 178 , 102 ).rgb(), { "://beige.png", 20 }}, { QColor(76 , 0 , 153 ).rgb(), { "://violet.png", 20 }}, { QColor(100 , 100 , 100 ).rgb(), { "://gris.png", 20 }}, }; bool IsCloseColor( QColor c1, QColor c2 ) { int diffRed = abs(c1.red() - c2.red()); int diffGreen = abs(c1.green() - c2.green()); int diffBlue = abs(c1.blue() - c2.blue()); if (diffBlue+diffRed+diffGreen< 100){ return true; } else{ return false; } } void MainWindow::on_verticalSlider_sliderMoved(int position) { ui->verticalSlider->setRange(1,50); z=position; }
main.cpp
#include "mainwindow.h" #include <QApplication> #include <QPixmap> #include <QCoreApplication> #include <QFile> #include <QString> #include <QDebug> #include <QTextStream> #include <QMap> #include <QMapIterator> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPixmap> #include <QPoint> #include <QSize> #include <iostream> #include <QMapIterator> #include <QMap> namespace Ui { class MainWindow; } struct CostInfo { QString ImageName; int cost; }; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_push_clicked(); void on_push2_clicked(); void on_verticalSlider_sliderMoved(int position); void on_verticalSlider_actionTriggered(int action); private: Ui::MainWindow *ui; QImage pixi; QPixmap pixa; float k; int a; int z=1; int b; }; #endif // MAINWINDOW_H
-
You should cleanup the casing of your variables and class member calls.
-
@Payx
in your definition ofCostInfo
, your variable is namedcost
notCost
the member
hasNext
andnext
belong toQMapIterator
, notQMap::iterator
read the document of QMapIterator and QMap::iterator for more information -
Hi
I move the code around so it compiled here.
https://www.dropbox.com/s/1z7lzjk4fqwxslz/mycolortest.zip?dl=0
Also change QMapIterator to a for each loop to get rid of QMapIterator.If you get any errors from this it, please list them exactly.
-
Well since you know what function it is, simply set a break point and single step through the code.
That way it will clearly show what line it crashes on. ;) -
I placed breakpoint on
for(int x = topLeft.x(); x < maxX; ++x) { for(int y = topLeft.y(); y < maxY; ++y) { image.setPixelColor(x, y, colour); foreach( QRgb key, Costs.keys() ) { QColor BaseColor( key ); if (Costs.contains( colour.rgb() ) || IsCloseColor(BaseColor, colour) ) { CostInfo& ci = Costs[colour.rgb()]; int Cost = ci.Cost; QPixmap pix( ci.ImageName ); }
because its new and before it worked.
And nothing who looks like a crash happened. So i don't understand, maybe it is just a big picture (540 * 540) and the app have difficult to do the code
EDIT : i tried to increase the rectangle size, it worked.
but dont replace the color by a picture
and it say that int cost is an unused variable
-
Hi
In the inner
CostInfo& ci = Costs[colour.rgb()];
int Cost = ci.Cost; // not used for anything
QPixmap pix( ci.ImageName ); // not used for anything
}it was just for getting the data from the Costs qmap.
You still need to code what ever you want to do with it :)
Currently it just check if color is a match and even if yes, then nothing. -
so if i understood well
int Cost = ci.Cost; /// the Cost in my Qmap
QPixmap pix( ci.ImageName ); /// the picture into a PixmapThe problem is that the rectangle that i use to calcul the DominantColor isn't a Label or something, so how can i replace a picture into a virtual rectangle ?
-
@Payx
Yes those are the values from the qmap.- so how can i replace a picture into a virtual rectangle
Sorry but Im not sure what kind of operation you want to do.
For each dominĂ¡nt color for an area. you want to paint all of that area
with this color? Like paint 0,0,40,40 with same color. ? -
My program already do that :
but what i want is that for each rectangle that my picture have, it will replace this rectangle of color by a picture in my QMap (like if the rectangle's color is near the red, it will replace by the picture fraise.png and add a Cost)
-
@Payx
You mean that the red squares would be pictures instead?
The painter has drawPixmap
http://doc.qt.io/qt-5/qpainter.html#drawPixmap
You can fit the image to the rect. -
All square would be picture, the picture depends of the color.
If its red we see in the QMap that the picture is fraise.png
if its green -> balle verte.pngi think i dont need "QRectF target" or "source"
because i have already this
for(int x = topLeft.x(); x < maxX; ++x) { for(int y = topLeft.y(); y < maxY; ++y)
when im on the for i'm already in the square.