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 Updated to NodeBB v4.3 + New Features

How to include .txt on Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
196 Posts 11 Posters 135.7k Views 3 Watching
  • 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 last edited by
    #99

    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.png

    i 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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #100

      ok. that sounds fine.
      I assume you need to scale the pictures down to fit in the
      area. ?
      You can use scaled function.

      QPixmap scaledPix = pix.scaled(   WIDTH, HEIGHT
                                     Qt::KeepAspectRatio,
                                     Qt::SmoothTransformation
                                     );
      
      painter.drawPixmap(QPoint(), scaledPix);
      
      1 Reply Last reply
      0
      • P Offline
        P Offline
        Payx
        wrote on last edited by Payx
        #101

        So i have to rezise the picture or it will do automaticaly ?

        i don't understand what the function do

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #102

          No, you will have to resize it, else it will be exactly as in the file.( I assume too big)
          the scaled function returns a copy of the pixmap that is scaled to
          WIDTH, HEIGHT. so it will fit in your area.

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

            so if my rectangle is 8*8 tell me if it's correct :

            QPixmap scaledPix = pix.scaled(   8, 8
                                           Qt::KeepAspectRatio,
                                           Qt::SmoothTransformation
                                           );
            
            painter.drawPixmap(QPoint(i,j), scaledPix);
            

            just that ?

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #104

              Yes , that should draw image as 8x8 at i,j.
              Note the flag Qt::KeepAspectRatio which means it might not be 8x8 directly but
              maybe 8x6 if the original images was not square. AspectRatio means keep the relation between height and width.

              A 1 Reply Last reply
              0
              • mrjjM mrjj

                Yes , that should draw image as 8x8 at i,j.
                Note the flag Qt::KeepAspectRatio which means it might not be 8x8 directly but
                maybe 8x6 if the original images was not square. AspectRatio means keep the relation between height and width.

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #105

                @mrjj 104 posts on this thread... you are a patient person mrjj. ;) I would have just written the program for him by now, lol.

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                P 1 Reply Last reply
                0
                • A ambershark

                  @mrjj 104 posts on this thread... you are a patient person mrjj. ;) I would have just written the program for him by now, lol.

                  P Offline
                  P Offline
                  Payx
                  wrote on last edited by Payx
                  #106

                  @ambershark Lol

                  I prefer learn instead of just don't understand what i'm doing, i know i'm very bad to programmation by now but maybe in few years i'il be like mrjj haha !

                  for the QPoint, i use it here :

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

                  but i use the QPixmap scaledPix .....
                  in my function

                  void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectangle, const QColor& colour) {
                  

                  so it tell that j and i was not declared in this scope

                  so i declared i and j in my .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;
                  };
                  
                  int j;
                  int i;
                  
                  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);
                  
                   private:
                    Ui::MainWindow* ui;
                    QImage pixi;
                    QPixmap pixa;
                    float k;
                    int a;
                  
                  
                    int z = 1;
                    int b;
                  };
                  
                  #endif // MAINWINDOW_HH
                  
                  

                  it will work?

                  //

                  it tell that painter was not declared too, so i search to the QPainter class,

                  it's "void QPainter::drawPixmap(const QPoint & point, const QPixmap & pixmap)" that i should use ?

                  A 1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #107

                    Hi
                    hehe @ambershark , yes that would be faster but in this case,
                    declaration and scopes are part of the exercise so hence the amount of posts. :)

                    @Payx
                    The j and is local variable in your for loops
                    Yes it will work to make them member of the class but you actually give them to
                    Remplissage via the QPoint(i,j)
                    Remplissage(pixi,QPoint(i,j),QSize(z,z),Couleurdominante(pixi,QPoint(i,j),QSize(z,z)));
                    so inside the Remplissage function, i and j are
                    int i = topLeft.x();
                    int j=topLeft.y();

                    • it tell that painter was not declared too, so i search to the QPainter class,
                      Did you #include<QPainter> ?

                    Calling it like
                    painter.drawPixmap(topLeft, scaledPix);
                    should work.

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

                      Yes i have already test with include QPainter, same error ^^

                      mrjjM 1 Reply Last reply
                      0
                      • P Payx

                        Yes i have already test with include QPainter, same error ^^

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

                        @Payx
                        oh did you create the painter first ?
                        Oh You need to create it first. as a variable.
                        QPainter painter(this);

                        But here comes an issue. you can only use a painter in a PaintEvent function.

                        So only other way is to have a Pixmap as member and then paint to it in Remplissage
                        and then later paint the pixmap in PaintEvent.

                        Anyway to paint to an image
                        QPixmap pix(500,500); // make sure size matches, might need to live as member in class for paintEvent
                        QPainter painter(&pix);// give pix to painter
                        ...
                        painter.drawPixmap(topLeft, scaledPix); //

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

                          ok but i have an other question,

                          in my QMap, i defined my picture "fraise.png" as a QString ImageName;

                          so when i write

                          QPixmap pix( ci.ImageName );
                          

                          it will create a Pixmap from my image ?

                          mrjjM 1 Reply Last reply
                          0
                          • P Payx

                            ok but i have an other question,

                            in my QMap, i defined my picture "fraise.png" as a QString ImageName;

                            so when i write

                            QPixmap pix( ci.ImageName );
                            

                            it will create a Pixmap from my image ?

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

                            @Payx
                            yes that will create a pixmap if the path to image is valid.

                            QPixmap pix( ci.ImageName );

                            BUT
                            you seems to store as e "fraise.png" ?
                            That wont work as there is no path so it cant find it.

                            However if you embed the images via qres file
                            then the syntax ":/"
                            will allow to load them.
                            ":/fraise.png"

                            http://doc.qt.io/qt-5/resources.html
                            These images are then embedded into the .exe file and can be loaded with ":/" in front.

                            If you want to use external files, you must think of how to handle the paths.

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

                              mrjj: SORRY i edited your post!. browser lag. cannot undo
                              Im very sorry.

                              • Yes i have already do that u explained to me before ^^
                                Oh. sorry. Well now we know its working :)
                                seems good. Note its single ":/" not "://"

                              and i dont understand the "Give pix to painter"

                              When you construct the painter you give it the widget normally
                              QPainter painter(this); <<< "this" being the widget pointer
                              but when we paint on image then we give pixmap instead of "this"
                              QPainter painter(&pix); // pix is pixmap and not widget
                              So basically we tell painter to draw on this picmap and not on a widget.

                              mrjjM 1 Reply Last reply
                              0
                              • P Payx

                                mrjj: SORRY i edited your post!. browser lag. cannot undo
                                Im very sorry.

                                • Yes i have already do that u explained to me before ^^
                                  Oh. sorry. Well now we know its working :)
                                  seems good. Note its single ":/" not "://"

                                and i dont understand the "Give pix to painter"

                                When you construct the painter you give it the widget normally
                                QPainter painter(this); <<< "this" being the widget pointer
                                but when we paint on image then we give pixmap instead of "this"
                                QPainter painter(&pix); // pix is pixmap and not widget
                                So basically we tell painter to draw on this picmap and not on a widget.

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

                                @Payx
                                So you last issue is the painting of the little image.

                                Since we are not allowed to paint inside Remplissage, you have 2 options.

                                Do this in paintEvent. ( meaning call Remplissage )
                                or
                                let Remplissage paint on a new pixmap and then later paint this new pix in
                                the real paintEvent function.

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

                                  What is the simpliest method ? ahah

                                  mrjjM 1 Reply Last reply
                                  0
                                  • P Payx

                                    What is the simpliest method ? ahah

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

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

                                    What is the simpliest method ? ahah

                                    I think to paint on image as you can then later just
                                    show in QLabel and might not even need to make a PaintEvent.
                                    This new image should be the size of the red rect one where u scan for colors.
                                    so you will draw the mini images on this new picmap and then its the final pixmap i assume.

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

                                      so on my rectangle i create a label and i display the pixmap in the label ?

                                      mrjjM 1 Reply Last reply
                                      0
                                      • P Payx

                                        so on my rectangle i create a label and i display the pixmap in the label ?

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

                                        @Payx
                                        I dont know what rectangle is.
                                        the QLabel should/could be placed on UI and after you created the final image and OUTSIDE
                                        of any loops
                                        you can do
                                        ui->Label->setPix thing.

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

                                          so in this loop

                                            for(int x = topLeft.x(); x < maxX; ++x) {
                                              for(int y = topLeft.y(); y < maxY; ++y) {
                                          

                                          i can include

                                          QLabel *label = new QLabel(this);
                                                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 );
                                          					QPixmap scaledPix = pix.scaled(   8, 8,
                                          																				 Qt::KeepAspectRatio,
                                          																				 Qt::SmoothTransformation
                                          																				 );
                                          
                                          ui->label->setPixmap(pix);
                                          

                                          ?

                                          mrjjM 1 Reply Last reply
                                          0

                                          • Login

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