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
Forum Update on Monday, May 27th 2025

How to include .txt on Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
196 Posts 11 Posters 135.3k 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 Offline
    P Offline
    Payx
    wrote on 28 Dec 2016, 22:50 last edited by
    #75

    i included, no change.

    How can i see if i have a c++ compiler ?

    M 1 Reply Last reply 29 Dec 2016, 07:15
    1
    • P Payx
      28 Dec 2016, 22:50

      i included, no change.

      How can i see if i have a c++ compiler ?

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Dec 2016, 07:15 last edited by mrjj
      #76

      @Payx
      Ok. No sure what goes wrongly for you. Seems to have CostInfo
      in .h and Costs QMap in cpp which should be fine.

      • How can i see if i have a c++ compiler ?
        c++11 compiler. Meaning a compiler that knows the new c++11 version.
        I assume you do. But if you are on linux or using old Qt it might not be the case.

      So what platform and Qt version?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Payx
        wrote on 29 Dec 2016, 12:20 last edited by
        #77

        i paste for you all my project so you can see exactly :

        .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
        

        .cpp (mainwidow)

        #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);
        void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);
        
        
        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);
        			QMapIterator<QRgb, CostInfo> i;
        			while (Costs.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();
        }
        

        I am on windows 10
        Qt 4.1.0

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Dec 2016, 13:14 last edited by
          #78

          It looks ok.
          What is the exact error you get?

          Also 4.1 is very old. Are you using old mingw too?

          Old compilers might not like the initialisation list/in place
          If you just do
          QMap<QRgb, CostInfo > Costs;

          Will it compile then?

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Payx
            wrote on 29 Dec 2016, 13:55 last edited by Payx
            #79

            i added

            QMap<QRgb, CostInfo > Costs;
            

            In mainwindow.cpp

            and my error is !

            ![http://hpics.li/b0d194a](image url)

            with only QMap ... it compile

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 29 Dec 2016, 21:08 last edited by
              #80

              QMapIterator<QRgb, CostInfo> i; is wrong

              It should be something like

              QMap<QRgb, CostInfo>::const_iterator i = Costs.constBegin();
              while (i.hasNext()) {
              //etc.
              }
              

              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 29 Dec 2016, 21:37 last edited by
                #81

                @SGaist Thank you ! i think it works

                ![http://hpics.li/8a8e235](image url)

                i don't know how to solve the 3 first error

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 29 Dec 2016, 21:40 last edited by
                  #82

                  Please, show the latest version of your code.

                  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 29 Dec 2016, 21:43 last edited by
                    #83

                    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
                    
                    
                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 29 Dec 2016, 23:20 last edited by
                      #84

                      You should cleanup the casing of your variables and class member calls.

                      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 29 Dec 2016, 23:32 last edited by
                        #85

                        How clean up all ?

                        F 1 Reply Last reply 30 Dec 2016, 06:48
                        0
                        • P Payx
                          29 Dec 2016, 23:32

                          How clean up all ?

                          F Offline
                          F Offline
                          Flotisable
                          wrote on 30 Dec 2016, 06:48 last edited by Flotisable
                          #86

                          @Payx
                          in your definition of CostInfo, your variable is named cost not Cost

                          the member hasNext and next belong to QMapIterator, not QMap::iterator
                          read the document of QMapIterator and QMap::iterator for more information

                          1 Reply Last reply
                          2
                          • P Offline
                            P Offline
                            Payx
                            wrote on 30 Dec 2016, 14:21 last edited by
                            #87

                            So the line

                            QMapIterator<QRgb, CostInfo> i(Costs);
                            

                            Is correct ?
                            ""
                            What is a redefinition of a QMap ? because it say that i have a redefinition of ""QMap<QRgb, CostInfo > Costs""

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 30 Dec 2016, 14:37 last edited by
                              #88

                              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.

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                Payx
                                wrote on 30 Dec 2016, 15:46 last edited by
                                #89

                                Thanks it compile,

                                the problem is that my window crash when i push the boutton who execute
                                " Remplissage(pixi, QPoint(i, j), QSize(z, z), Couleurdominante(pixi, QPoint(i, j), QSize(z, z)));
                                "
                                ^^

                                maybe enough things to do ?

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 30 Dec 2016, 15:59 last edited by
                                  #90

                                  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. ;)

                                  1 Reply Last reply
                                  1
                                  • P Offline
                                    P Offline
                                    Payx
                                    wrote on 30 Dec 2016, 16:20 last edited by
                                    #91

                                    It's not crash, the program run at infinity and do nothing

                                    M 1 Reply Last reply 30 Dec 2016, 16:33
                                    0
                                    • P Payx
                                      30 Dec 2016, 16:20

                                      It's not crash, the program run at infinity and do nothing

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 30 Dec 2016, 16:33 last edited by
                                      #92

                                      @Payx
                                      well that will also be really clear from single stepping :)

                                      1 Reply Last reply
                                      1
                                      • P Offline
                                        P Offline
                                        Payx
                                        wrote on 30 Dec 2016, 16:59 last edited by Payx
                                        #93

                                        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

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 30 Dec 2016, 18:00 last edited by
                                          #94

                                          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.

                                          1 Reply Last reply
                                          0

                                          84/196

                                          29 Dec 2016, 23:20

                                          • Login

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