Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to include .txt on Qt

How to include .txt on Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
196 Posts 11 Posters 135.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Payx

    EDIT :

    i replaced this line "diffBlue + diffRed + diffGreen < 100" 100 by 450 and increase the picture to a 20 * 20

    i have :

    image i get from ci.ImageName :  ":/img/fraise.png"
    setGeometry: Unable to set geometry 20x16+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x16+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/rose.png"
    setGeometry: Unable to set geometry 20x14+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x14+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/ballepeche.png"
    setGeometry: Unable to set geometry 19x20+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x20+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    yep its no good
    yep its no good
    its OK with base color
    image i get from ci.ImageName :  ":/img/noir.png"
    setGeometry: Unable to set geometry 20x17+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x17+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/balebleue.png"
    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).
    its OK with base color
    image i get from ci.ImageName :  ":/img/turquoise.png"
    setGeometry: Unable to set geometry 11x20+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x20+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/balleverte.png"
    setGeometry: Unable to set geometry 20x15+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x15+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/violet.png"
    setGeometry: Unable to set geometry 15x20+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x20+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/gris.png"
    setGeometry: Unable to set geometry 16x20+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x20+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/marron.png"
    setGeometry: Unable to set geometry 8x20+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x20+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/fraise.png"
    setGeometry: Unable to set geometry 20x16+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x16+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/rose.png"
    setGeometry: Unable to set geometry 20x14+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x14+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    its OK with base color
    image i get from ci.ImageName :  ":/img/ballepeche.png"
    setGeometry: Unable to set geometry 19x20+640+280 on QWidgetWindow/'QLabelClassWindow'. Resulting geometry:  120x20+640+280 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
    yep its no good
    yep its no good
    its OK with base color
    image i get from ci.ImageName :  ":/img/noir.png"
    

    its better no ?

    and i had already changed into a

    					if (pix.isNull()){
    					qDebug() << "nothing";
    					return;
    					};
    

    to see exactly where it bug

    EDIT : it will not be displayed on my label, so if i put my the definition of my label in my .h doest it work?

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #176

    @Payx
    Well it seems you just do
    QLabel abc;
    which will not work at all as this label is then deleted as soon as function ends.

    Putting it in .h will help it not getting deleted but its still not connected to the screen in any way.
    If you have UI file. Cant u put it there?
    and get via
    ui->label
    Then its in the right place.

    To do this, you must make Remplissage member of the mainwindow so you can acces its UI

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Payx
      wrote on last edited by
      #177

      Yes but if i create a label in my UI the picture will just be displayed in the same label no ?

      mrjjM 1 Reply Last reply
      0
      • P Payx

        Yes but if i create a label in my UI the picture will just be displayed in the same label no ?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #178

        @Payx said in How to include .txt on Qt:

        a label in my UI the picture will just be displayed in the same label no ?

        Yes. You can add more than one label if u need more pics to be shown at same time.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Payx
          wrote on last edited by
          #179

          my funtion is

          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) {
          

          in

          Remplissage(pixi, QPoint(i, j), QSize(z, z), Couleurdominante(pixi, QPoint(i, j), QSize(z, z)));
          

          so it depends on my z, if i say that my z = 20; and if my picture is 540 * 540 i will need 729 labels lol.

          example :

          here my previous code : http://hpics.li/36d1f82

          my picture is 128 * 128, if my z = 28; it appears 4 colors by 4 colors.

          How can i display a picture by color without add 10000 labels, thats why i though that create a label in my loop will work

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #180

            Create a new image and put it into a secondary QLabel.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Payx
              wrote on last edited by Payx
              #181

              Something like :
              QImage from pix
              And put the image into a label ?

              I know that put a label into a loop wont work u told me. But how can i do so ?

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Payx
                wrote on last edited by Payx
                #182

                nobody know how to ?

                or maybe an other way, if its too complicated ^^

                mrjjM 1 Reply Last reply
                0
                • P Payx

                  nobody know how to ?

                  or maybe an other way, if its too complicated ^^

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #183

                  @Payx
                  You lost me on what you are trying with the Labels. 729 ?!?!
                  Do you mean to have the lables like
                  alt text
                  or how would you place all these labels?

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Payx
                    wrote on last edited by
                    #184

                    label is not obligatory, it's just me with my few knowlegde that i said i can use labels.

                    but the problem is that i have to replace the color by the picture (replace or add) and its here that i'm blocked

                    mrjjM 1 Reply Last reply
                    0
                    • P Payx

                      label is not obligatory, it's just me with my few knowlegde that i said i can use labels.

                      but the problem is that i have to replace the color by the picture (replace or add) and its here that i'm blocked

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #185

                      @Payx
                      on top of the "red" image ? so that each cell is an image?

                      1 Reply Last reply
                      1
                      • P Offline
                        P Offline
                        Payx
                        wrote on last edited by Payx
                        #186

                        yes each cell is an image.

                        on the picture that you showed there is 16 cell so 16 picture who depend on the color

                        Edit : we had before "image.setPixelColor(x, y, colour);"

                        can't we have like image.'setImage' or something?

                        FlotisableF 1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          Payx
                          wrote on last edited by Payx
                          #187

                          i changed a lot in my code with research :

                          (.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>
                          #include <QDebug>
                          #include <QGraphicsItem>
                          #include <QGraphicsScene>
                          using namespace std;
                          
                          // forwards
                          QColor Couleurdominante(const QImage& image, const QPoint& topLeft, const QSize& rectSize);
                          void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);
                          bool IsCloseColor( QColor c1, QColor c2 );
                          void Remplacement (const QColor& colour, const QPoint& topLeft, QImage& image);
                          //-----------------------------------------------------
                          
                          QMap<QRgb, CostInfo > Costs = {
                          	{ QColor(255 , 0 , 0 ).rgb(), { ":/img/fraise.png", 10 }},
                          	{ QColor(0 , 255 , 0 ).rgb(), { ":/img/balleverte.png", 20 }},
                          	{ QColor(0 , 0 , 255 ).rgb(), { ":/img/ballebleue.png", 20 }},
                          	{ QColor(255 , 255 , 255 ).rgb(), { ":/img/balleblanche.png", 20 }},
                          	{ QColor(255 , 128 , 0 ).rgb(), { ":/img/ballepeche.png", 20 }},
                          	{ QColor(0 , 0 , 0 ).rgb(), { ":/img/noir.png", 20 }},
                          	{ QColor(102 , 51 , 0 ).rgb(), { ":/img/marron.png", 20 }},
                          	{ QColor(255 , 102 , 78 ).rgb(), { ":/img/rose.png", 20 }},
                          	{ QColor(0 , 204 , 204 ).rgb(), { ":/img/turquoise.png", 20 }},
                          	{ QColor(255 , 178 , 102 ).rgb(), { ":/img/beige.png", 20 }},
                          	{ QColor(76 , 0 , 153 ).rgb(), { ":/img/violet.png", 20 }},
                          	{ QColor(100 , 100 , 100 ).rgb(), { ":/img/gris.png", 20 }},
                          };
                          
                          
                          
                          //-----------------------------------------------------
                          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);
                          	ui->label->setFixedHeight(pix.height());
                          	ui->label->setFixedWidth(pix.width());
                          	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() {
                          
                          	int z = 20 ;
                          	for (int i = 0; i < pixi.width(); i += z) {
                          		for (int j = 0; j < pixi.height(); j += z) {
                          
                          			Remplissage(pixi, QPoint(i, j), QSize(z, z), Couleurdominante(pixi, QPoint(i, j), QSize(z, z)));
                          			Remplacement(Couleurdominante(pixi, QPoint(i, j), QSize(z, z)), QPoint(i,j),pixi );
                          
                          		}
                          	}
                          
                          	pixa = QPixmap::fromImage(pixi);
                          	ui->label_3->setPixmap(pixa);
                          	ui->label_3->setFixedHeight(pixa.height());
                          	ui->label_3->setFixedWidth(pixa.width());
                          
                          
                          }
                          //-----------------------------------------------------
                          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 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);
                          }
                          //-----------------------------------------------------
                          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 < 350) {
                          		return true;
                          
                          	}	else {
                          		return false;
                          
                          
                          	}
                          }
                          //-----------------------------------------------------
                          void MainWindow::on_verticalSlider_sliderMoved(int position) {
                          }
                          
                          void Remplacement (const QColor& colour, const QPoint& topLeft, QImage& image){
                          
                          if (Costs.contains( colour.rgb() ))  {
                          										CostInfo& ci = Costs[colour.rgb()];
                          									//          int Cost = ci.Cost;
                          										qDebug() << "check me--->" << ci.ImageName;
                          										QPixmap pix( ci.ImageName );
                          										if (pix.isNull()){
                          										qDebug() << "nothing";
                          										return;
                          										};
                          										QPixmap scaledPix = pix.scaled(   20, 20,
                          																									 Qt::KeepAspectRatio,
                          																									 Qt::SmoothTransformation
                          																									 );
                          										QGraphicsPixmapItem *item = new QGraphicsPixmapItem(scaledPix);
                          										scene.addItem(item);
                          										item->setPos(topLeft.x() , topLeft.y());
                          
                          }
                          else {
                          										foreach( QRgb key, Costs.keys() ) {
                          										QColor BaseColor( key );
                          										if (IsCloseColor(BaseColor, colour) == true){
                          											if ( Costs.contains( BaseColor.rgb())){
                          																qDebug () << "its OK with base color";
                          																}else{
                          																qDebug () << " base color had no match!!";
                          															}
                          
                          											CostInfo& ci = Costs[BaseColor.rgb()];
                          											QPixmap pix( ci.ImageName );
                          											qDebug() << "image i get from ci.ImageName : " << ci.ImageName;
                          											QPixmap scaledPix = pix.scaled(   20, 20,
                          																										 Qt::KeepAspectRatio,
                          																										 Qt::SmoothTransformation
                          																										 );
                          											QGraphicsPixmapItem *item = new QGraphicsPixmapItem(scaledPix);
                          											scene.addItem(item);
                          											item->setPos(topLeft.x() , topLeft.y());
                          	}
                          	else {
                          		qDebug() << "yep its no good";
                          	}
                          }
                          
                          }
                          }
                          
                          
                          

                          i find a method for display my image or pix (i think) but i'm blocked with the scene ... tell me if i'm in the right direction

                          1 Reply Last reply
                          0
                          • P Payx

                            yes each cell is an image.

                            on the picture that you showed there is 16 cell so 16 picture who depend on the color

                            Edit : we had before "image.setPixelColor(x, y, colour);"

                            can't we have like image.'setImage' or something?

                            FlotisableF Offline
                            FlotisableF Offline
                            Flotisable
                            wrote on last edited by
                            #188

                            @Payx
                            this is what I think before
                            I think we can make a method like setImage by scaling the origin image and then set the pixel according to the image that is read

                            and I'll read your code
                            by the way, can you modify your indentation? I find it's hard to read your code with too many tabs, I need to scroll from left to right.

                            1 Reply Last reply
                            1
                            • P Offline
                              P Offline
                              Payx
                              wrote on last edited by
                              #189
                              #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>
                              #include <QDebug>
                              #include <QGraphicsItem>
                              #include <QGraphicsScene>
                              using namespace std;
                              
                              // forwards
                              QColor Couleurdominante(const QImage& image, const QPoint& topLeft, const QSize& rectSize);
                              void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);
                              bool IsCloseColor( QColor c1, QColor c2 );
                              void Remplacement (const QColor& colour, const QPoint& topLeft, QImage& image);
                              QGraphicsScene scene;
                              //-----------------------------------------------------
                              
                              QMap<QRgb, CostInfo > Costs = {
                              	{ QColor(255 , 0 , 0 ).rgb(), { ":/img/fraise.png", 10 }},
                              	{ QColor(0 , 255 , 0 ).rgb(), { ":/img/balleverte.png", 20 }},
                              	{ QColor(0 , 0 , 255 ).rgb(), { ":/img/ballebleue.png", 20 }},
                              	{ QColor(255 , 255 , 255 ).rgb(), { ":/img/balleblanche.png", 20 }},
                              	{ QColor(255 , 128 , 0 ).rgb(), { ":/img/ballepeche.png", 20 }},
                              	{ QColor(0 , 0 , 0 ).rgb(), { ":/img/noir.png", 20 }},
                              	{ QColor(102 , 51 , 0 ).rgb(), { ":/img/marron.png", 20 }},
                              	{ QColor(255 , 102 , 78 ).rgb(), { ":/img/rose.png", 20 }},
                              	{ QColor(0 , 204 , 204 ).rgb(), { ":/img/turquoise.png", 20 }},
                              	{ QColor(255 , 178 , 102 ).rgb(), { ":/img/beige.png", 20 }},
                              	{ QColor(76 , 0 , 153 ).rgb(), { ":/img/violet.png", 20 }},
                              	{ QColor(100 , 100 , 100 ).rgb(), { ":/img/gris.png", 20 }},
                              };
                              
                              
                              
                              //-----------------------------------------------------
                              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);
                              	ui->label->setFixedHeight(pix.height());
                              	ui->label->setFixedWidth(pix.width());
                              	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() {
                              
                              	int z = 20 ;
                              	for (int i = 0; i < pixi.width(); i += z) {
                              		for (int j = 0; j < pixi.height(); j += z) {
                              
                              			Remplissage(pixi, QPoint(i, j), QSize(z, z), Couleurdominante(pixi, QPoint(i, j), QSize(z, z)));
                              			Remplacement(Couleurdominante(pixi, QPoint(i, j), QSize(z, z)), QPoint(i,j),pixi );
                              
                              		}
                              	}
                              
                              	pixa = QPixmap::fromImage(pixi);
                              	ui->label_3->setPixmap(pixa);
                              	ui->label_3->setFixedHeight(pixa.height());
                              	ui->label_3->setFixedWidth(pixa.width());
                              
                              
                              }
                              //-----------------------------------------------------
                              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 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);
                              }
                              //-----------------------------------------------------
                              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 < 350) {
                              		return true;
                              
                              	}	else {
                              		return false;
                              
                              
                              	}
                              }
                              //-----------------------------------------------------
                              void MainWindow::on_verticalSlider_sliderMoved(int position) {
                              }
                              
                              void Remplacement (const QColor& colour, const QPoint& topLeft, QImage& image){
                              
                              if (Costs.contains( colour.rgb() ))  {
                              					CostInfo& ci = Costs[colour.rgb()];
                              									//          int Cost = ci.Cost;
                              				qDebug() << "check me--->" << ci.ImageName;
                              				QPixmap pix( ci.ImageName );
                              						if (pix.isNull()){
                              							qDebug() << "nothing";
                              						return;
                              						};
                              		QPixmap scaledPix = pix.scaled(   20, 20,
                              																			 Qt::KeepAspectRatio,
                              																			 Qt::SmoothTransformation
                              																				 );
                              		QGraphicsPixmapItem *item = new QGraphicsPixmapItem(scaledPix);
                              			scene.addItem(item);
                              			item->setPos(topLeft.x() , topLeft.y());
                              
                              }
                              else {
                              					foreach( QRgb key, Costs.keys() ) {
                              					QColor BaseColor( key );
                              					if (IsCloseColor(BaseColor, colour) == true){
                              					if ( Costs.contains( BaseColor.rgb())){
                              			               qDebug () << "its OK with base color";
                              					}else{
                              					qDebug () << " base color had no match!!";
                              								}
                              
                              		  CostInfo& ci = Costs[BaseColor.rgb()];
                              			QPixmap pix( ci.ImageName );
                              			qDebug() << "image i get from ci.ImageName : " << ci.ImageName;
                              			QPixmap scaledPix = pix.scaled(   20, 20,
                              										 Qt::KeepAspectRatio,
                              								        	 Qt::SmoothTransformation
                              												 );
                              					QGraphicsPixmapItem *item = new QGraphicsPixmapItem(scaledPix);
                              					scene.addItem(item);
                              			item->setPos(topLeft.x() , topLeft.y());
                              	}
                              	else {
                              		qDebug() << "yep its no good";
                              	}
                              }
                              
                              }
                              }
                              
                              
                              

                              here my code.

                              My original image is already scaled with

                              	for (int i = 0; i < pixi.width(); i += z) {
                              		for (int j = 0; j < pixi.height(); j += z) {
                              

                              so i just add one function to replace rectangle of color by the picture who correspond to this color

                              FlotisableF 1 Reply Last reply
                              0
                              • P Payx
                                #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>
                                #include <QDebug>
                                #include <QGraphicsItem>
                                #include <QGraphicsScene>
                                using namespace std;
                                
                                // forwards
                                QColor Couleurdominante(const QImage& image, const QPoint& topLeft, const QSize& rectSize);
                                void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);
                                bool IsCloseColor( QColor c1, QColor c2 );
                                void Remplacement (const QColor& colour, const QPoint& topLeft, QImage& image);
                                QGraphicsScene scene;
                                //-----------------------------------------------------
                                
                                QMap<QRgb, CostInfo > Costs = {
                                	{ QColor(255 , 0 , 0 ).rgb(), { ":/img/fraise.png", 10 }},
                                	{ QColor(0 , 255 , 0 ).rgb(), { ":/img/balleverte.png", 20 }},
                                	{ QColor(0 , 0 , 255 ).rgb(), { ":/img/ballebleue.png", 20 }},
                                	{ QColor(255 , 255 , 255 ).rgb(), { ":/img/balleblanche.png", 20 }},
                                	{ QColor(255 , 128 , 0 ).rgb(), { ":/img/ballepeche.png", 20 }},
                                	{ QColor(0 , 0 , 0 ).rgb(), { ":/img/noir.png", 20 }},
                                	{ QColor(102 , 51 , 0 ).rgb(), { ":/img/marron.png", 20 }},
                                	{ QColor(255 , 102 , 78 ).rgb(), { ":/img/rose.png", 20 }},
                                	{ QColor(0 , 204 , 204 ).rgb(), { ":/img/turquoise.png", 20 }},
                                	{ QColor(255 , 178 , 102 ).rgb(), { ":/img/beige.png", 20 }},
                                	{ QColor(76 , 0 , 153 ).rgb(), { ":/img/violet.png", 20 }},
                                	{ QColor(100 , 100 , 100 ).rgb(), { ":/img/gris.png", 20 }},
                                };
                                
                                
                                
                                //-----------------------------------------------------
                                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);
                                	ui->label->setFixedHeight(pix.height());
                                	ui->label->setFixedWidth(pix.width());
                                	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() {
                                
                                	int z = 20 ;
                                	for (int i = 0; i < pixi.width(); i += z) {
                                		for (int j = 0; j < pixi.height(); j += z) {
                                
                                			Remplissage(pixi, QPoint(i, j), QSize(z, z), Couleurdominante(pixi, QPoint(i, j), QSize(z, z)));
                                			Remplacement(Couleurdominante(pixi, QPoint(i, j), QSize(z, z)), QPoint(i,j),pixi );
                                
                                		}
                                	}
                                
                                	pixa = QPixmap::fromImage(pixi);
                                	ui->label_3->setPixmap(pixa);
                                	ui->label_3->setFixedHeight(pixa.height());
                                	ui->label_3->setFixedWidth(pixa.width());
                                
                                
                                }
                                //-----------------------------------------------------
                                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 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);
                                }
                                //-----------------------------------------------------
                                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 < 350) {
                                		return true;
                                
                                	}	else {
                                		return false;
                                
                                
                                	}
                                }
                                //-----------------------------------------------------
                                void MainWindow::on_verticalSlider_sliderMoved(int position) {
                                }
                                
                                void Remplacement (const QColor& colour, const QPoint& topLeft, QImage& image){
                                
                                if (Costs.contains( colour.rgb() ))  {
                                					CostInfo& ci = Costs[colour.rgb()];
                                									//          int Cost = ci.Cost;
                                				qDebug() << "check me--->" << ci.ImageName;
                                				QPixmap pix( ci.ImageName );
                                						if (pix.isNull()){
                                							qDebug() << "nothing";
                                						return;
                                						};
                                		QPixmap scaledPix = pix.scaled(   20, 20,
                                																			 Qt::KeepAspectRatio,
                                																			 Qt::SmoothTransformation
                                																				 );
                                		QGraphicsPixmapItem *item = new QGraphicsPixmapItem(scaledPix);
                                			scene.addItem(item);
                                			item->setPos(topLeft.x() , topLeft.y());
                                
                                }
                                else {
                                					foreach( QRgb key, Costs.keys() ) {
                                					QColor BaseColor( key );
                                					if (IsCloseColor(BaseColor, colour) == true){
                                					if ( Costs.contains( BaseColor.rgb())){
                                			               qDebug () << "its OK with base color";
                                					}else{
                                					qDebug () << " base color had no match!!";
                                								}
                                
                                		  CostInfo& ci = Costs[BaseColor.rgb()];
                                			QPixmap pix( ci.ImageName );
                                			qDebug() << "image i get from ci.ImageName : " << ci.ImageName;
                                			QPixmap scaledPix = pix.scaled(   20, 20,
                                										 Qt::KeepAspectRatio,
                                								        	 Qt::SmoothTransformation
                                												 );
                                					QGraphicsPixmapItem *item = new QGraphicsPixmapItem(scaledPix);
                                					scene.addItem(item);
                                			item->setPos(topLeft.x() , topLeft.y());
                                	}
                                	else {
                                		qDebug() << "yep its no good";
                                	}
                                }
                                
                                }
                                }
                                
                                
                                

                                here my code.

                                My original image is already scaled with

                                	for (int i = 0; i < pixi.width(); i += z) {
                                		for (int j = 0; j < pixi.height(); j += z) {
                                

                                so i just add one function to replace rectangle of color by the picture who correspond to this color

                                FlotisableF Offline
                                FlotisableF Offline
                                Flotisable
                                wrote on last edited by
                                #190

                                @Payx
                                so where is the scene? I don't find any declaration.
                                and I don't find a way to convert a QGraphicsScene to a QImage

                                I have two directions

                                1. if you want to use the QGraphicsScene, then you don't need to convert it into a image. just use the scene to show your image, and you can read Graphics View Framework to see how to use it.
                                2. just copy the scaledPix pixel by pixel to the image
                                1 Reply Last reply
                                1
                                • P Offline
                                  P Offline
                                  Payx
                                  wrote on last edited by Payx
                                  #191

                                  i put my .h to see where i declared my scene

                                  #ifndef MAINWINDOW_H
                                  #define MAINWINDOW_H
                                  
                                  #include <QMainWindow>
                                  #include <QPixmap>
                                  #include <QPoint>
                                  #include <QSize>
                                  #include <iostream>
                                  #include <QMapIterator>
                                  #include <QMap>
                                  #include <QDebug>
                                  #include <QGraphicsItem>
                                  #include <QGraphicsScene>
                                  
                                  namespace Ui {
                                  	class MainWindow;
                                  }
                                  struct CostInfo
                                  {
                                  	QString ImageName;
                                  	int     Cost;
                                  };
                                  
                                  
                                  class MainWindow : public QMainWindow
                                  {
                                  	Q_OBJECT
                                  
                                  public:
                                  		QPixmap pixa;
                                  	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;
                                  	QGraphicsScene scene;
                                  	QImage pixi;
                                  
                                  	float k;
                                  	int a;
                                  
                                  
                                  
                                  	int z=1;
                                  	int b;
                                  };
                                  
                                  #endif // MAINWINDOW_H
                                  
                                  

                                  it will work?

                                  EDIT : It compile but my widow crashed when i run it and nothing happend

                                  FlotisableF 1 Reply Last reply
                                  0
                                  • P Payx

                                    i put my .h to see where i declared my scene

                                    #ifndef MAINWINDOW_H
                                    #define MAINWINDOW_H
                                    
                                    #include <QMainWindow>
                                    #include <QPixmap>
                                    #include <QPoint>
                                    #include <QSize>
                                    #include <iostream>
                                    #include <QMapIterator>
                                    #include <QMap>
                                    #include <QDebug>
                                    #include <QGraphicsItem>
                                    #include <QGraphicsScene>
                                    
                                    namespace Ui {
                                    	class MainWindow;
                                    }
                                    struct CostInfo
                                    {
                                    	QString ImageName;
                                    	int     Cost;
                                    };
                                    
                                    
                                    class MainWindow : public QMainWindow
                                    {
                                    	Q_OBJECT
                                    
                                    public:
                                    		QPixmap pixa;
                                    	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;
                                    	QGraphicsScene scene;
                                    	QImage pixi;
                                    
                                    	float k;
                                    	int a;
                                    
                                    
                                    
                                    	int z=1;
                                    	int b;
                                    };
                                    
                                    #endif // MAINWINDOW_H
                                    
                                    

                                    it will work?

                                    EDIT : It compile but my widow crashed when i run it and nothing happend

                                    FlotisableF Offline
                                    FlotisableF Offline
                                    Flotisable
                                    wrote on last edited by
                                    #192

                                    @Payx
                                    you can use the debug mode to find out which line of code makes it crash.

                                    and I find that you declare two scene, one in the MainWindow, one is global, and you did not use the scene in the MainWindow.

                                    1 Reply Last reply
                                    0
                                    • P Offline
                                      P Offline
                                      Payx
                                      wrote on last edited by
                                      #193

                                      ok but i keep just the scene in my .h but it tell me that scene isnt declared

                                      1 Reply Last reply
                                      0
                                      • P Offline
                                        P Offline
                                        Payx
                                        wrote on last edited by
                                        #194

                                        Can someone put the topic as "solved" my problem isnt the same at the beginning i will create an other tread

                                        1 Reply Last reply
                                        -1
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #195

                                          You can do it, click on the "Topic Tools" button, you have there the "Mark as solved" entry.

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          2

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved