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.4k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    What kind of picture do you have in mind ? What size ?

    @darius Please don't highjack other's people thread with unrelated questions.

    When you create a new topic you have line edit which contains "Enter your topic title here..."
    If you want to edit late use the three doted menu on the far right and you'll have your topic title editable at the same place.

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

    D 1 Reply Last reply
    1
    • K Offline
      K Offline
      KzR69100
      wrote on last edited by
      #6

      it depends, but at the end i will have only 40 - 50 squares with just one color in them.

      So what i want is that Qt replace a color by a picture, ( so i will have 40 - 50 pictures ) and a total price (like 100 dollars)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LuGRU
        wrote on last edited by
        #7

        @KzR69100
        My 2 cents :)

        I would go with custom class / structure and serialization. That way there is no need for any kind of database / parsing (per say).
        Also id squares not by color, because after fill You will loose that color, but by some sort of id and keep it's rectangle.

        1 Reply Last reply
        0
        • BuckwheatB Offline
          BuckwheatB Offline
          Buckwheat
          wrote on last edited by
          #8

          @KzR69100 ... @LuGRU gives good advice. But, if you use an ID then your database idea would work as you could make the ID the table entry ID.

          The only recommendation I have is to look at what you are trying to solve. What is the future of what you are trying to solve. Then, pick the appropriate solution. I have seen too many times solutions that don't fit the problem to solve because of a "cool tool" or package someone just had to use.

          Dave Fileccia

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KzR69100
            wrote on last edited by KzR69100
            #9

            I have a function who return me a color (QColor function)

            QColor Color(...){
            
            return color;
            }
            

            like that, and i thought that after the "return color" i will have a "if color == red, add 6 dollars, and replace it by a picture"

            the problem is that i will have to do this with many colors so it will take a lot of place in my code, thats why i thought use a database that i will include in my Qt who contained a sort of "x = add 6 dollars, and replace it by a picture"

            so i will just have to do :

            if color == red, red = x ;

            is this possible ?

            1 Reply Last reply
            0
            • SGaistS SGaist

              What kind of picture do you have in mind ? What size ?

              @darius Please don't highjack other's people thread with unrelated questions.

              When you create a new topic you have line edit which contains "Enter your topic title here..."
              If you want to edit late use the three doted menu on the far right and you'll have your topic title editable at the same place.

              D Offline
              D Offline
              darius
              wrote on last edited by
              #10

              @SGaist said in How to include .txt on QT:

              When you create a new topic you have line edit which contains "Enter your topic title here..."

              When I was trying to create a new topic for the last 3 days, forum.qt.io opened in Firefox (latest version) for Windows with oversized font header and blue header frame was made of two lines.
              Topic input frame was lost due to the bug in html code.

              In the meantime I have notified Qt about the bug and today Input box for topic title is back.
              There is another bug in html code visible in Firefox, since there is no send/post button
              I assume, an arrow in upper right corner should replace this button (not easy or user-friendly solution)

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

                @darius Can't reproduce that on Firefox on macOS, the Submit combo is sitting there on the far right.

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

                D 1 Reply Last reply
                0
                • SGaistS SGaist

                  @darius Can't reproduce that on Firefox on macOS, the Submit combo is sitting there on the far right.

                  D Offline
                  D Offline
                  darius
                  wrote on last edited by
                  #12

                  @SGaist thank you. I use upper right arrow on blue background to send this reply.
                  Thank you for your time.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KzR69100
                    wrote on last edited by
                    #13

                    And for my problem ? :p

                    mrjjM 1 Reply Last reply
                    1
                    • K KzR69100

                      And for my problem ? :p

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

                      @KzR69100

                      So what u want is

                      RGB value, ImageName, Cost

                      So for a given RGB value, u want to look up ImageName, and Cost?

                      How many of those looks up will you need?
                      If only 10 or 15 , then a DB might be a bit overkill.

                      Do you need it to be dynamic or could it be 100% predefined?

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        KzR69100
                        wrote on last edited by
                        #15

                        """"So for a given RGB value, u want to look up ImageName, and Cost?"""

                        Yes exactly,

                        i got 10 pictures who got 1 colors and 5 - 6 pictures who got 2 colors (if 2 squares beside equal got the same color as the picture who got 2 colors, so i will do 2 square = 1 picture)

                        but the principal is the 10 picture who got 1 color.

                        It will be all predefined no dynamic

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

                          Which size should these picture be ?

                          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
                          • K KzR69100

                            """"So for a given RGB value, u want to look up ImageName, and Cost?"""

                            Yes exactly,

                            i got 10 pictures who got 1 colors and 5 - 6 pictures who got 2 colors (if 2 squares beside equal got the same color as the picture who got 2 colors, so i will do 2 square = 1 picture)

                            but the principal is the 10 picture who got 1 color.

                            It will be all predefined no dynamic

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

                            @KzR69100
                            Hi
                            You can make such small number very compact in code.
                            Add the images to a qres file and store name as :/imagename and
                            you can get a pixmap directly from it. ( as in Pixmap p(":/img");
                            Sample code. very basic but could be enough for your case.

                            struct CostInfo {
                              QString ImageName;
                              int Cost;
                            };
                            
                            QMap<QRgb, CostInfo > Costs = {
                              { QColor(255, 0, 120).rgb(), { ":/imageinres", 10 }},
                              { QColor(110, 255, 110).rgb(), { "xxxx", 20 }},
                              { QColor(120, 255, 20).rgb(), { "xxxx", 30 }},
                              { QColor(130, 255, 90).rgb(), { "xxxx", 40 }},
                              { QColor(140, 255, 10).rgb(), { "xxxx", 50 }},
                              { QColor(100, 100, 100).rgb(), { "MEEEE", 60 }},
                              { QColor(110, 255, 40).rgb(), { "xxxx", 70 }},
                              { QColor(210, 255, 50).rgb(), { "xxxx", 80 }},
                              { QColor(30, 255, 60).rgb(), { "xxxx", 3310 }},
                              { QColor(20, 255, 70).rgb(), { "xxxx", 4410 }},
                              { QColor(40, 255, 80).rgb(), { "xxx", 150 }},
                              { QColor(50, 255, 90).rgb(), { "xxx", 160 }}
                            };
                            
                            
                            void Test() {
                              QColor col = QColor(100, 100, 100); // getColor()..
                              if (Costs.contains( col.rgb() )) {
                                CostInfo& ci = Costs[col.rgb()];
                                qDebug() << "found:" << "cost:" << ci.Cost << "img:" << ci.ImageName;
                              }
                            }
                            
                            

                            Running test() gives
                            found: cost: 60 img: "MEEEE"

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              KzR69100
                              wrote on last edited by
                              #18

                              @SGaist i don't know it depends as i said, but in all case i think it will contain like 50 squares.

                              @mrjj Thanks for your help.
                              I don't understand the " QMap<QRgb, CostInfo > Costs "

                              and my problem i think, it's if i got a QColor(105,4,85) there will be a problem because it will not found a QColor who corresponds.

                              So i thought use a "for" to take a value range, and have all possible color, that's correct ?

                              1 Reply Last reply
                              0
                              • mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #19
                                • I don't understand the " QMap<QRgb, CostInfo > Costs "
                                  Its a associative list. Using QRgb ( the color ) as key and it returns a Costs struct that is the
                                  data associated with that key.
                                  Read about it here
                                  http://doc.qt.io/qt-5/qmap.html
                                  Its a lookup list so to speak.

                                • and my problem i think, it's if i got a QColor(105,4,85) there will be a problem because it will not found a QColor who corresponds.

                                Well if not in list, { QColor(105,4,85).rgb(), { "xxxx", 50 }},
                                then nothing happens as you test with
                                if (Costs.contains( col.rgb() )) { << test if found
                                xxxonly do if foundxxx

                                1 Reply Last reply
                                0
                                • K Offline
                                  K Offline
                                  KzR69100
                                  wrote on last edited by
                                  #20

                                  @mrjj
                                  So if nothing happens that's bad :p

                                  What do you think about use a "for" ?

                                  Like i don't care if it's a dark red, or a simple red etc..

                                  My question is :

                                  If nothing if found, how function can i use to find the "closer" color to the define color in my struct ?

                                  thanks

                                  1 Reply Last reply
                                  0
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #21
                                    • If nothing if found, how function can i use to find the "closer" color to the define color in my struct ?

                                    Hi
                                    use the QMapIterator instead of "for"

                                    QMapIterator<QString, int> i(map);
                                    while (i.hasNext()) {
                                    i.next();
                                    cout << i.key() << ": " << i.value() << endl;
                                    }
                                    http://doc.qt.io/qt-5/qmap.html#details

                                    That allows you to go over all and get the keys. (i.key())
                                    QColor TheColor(i.key())
                                    Then you can calculate if the RGB values for TheColor is
                                    close enough for you to accept it.
                                    For inspiration see here
                                    http://stackoverflow.com/questions/9018016/how-to-compare-two-colors

                                    int diffRed   = Math.abs(c1.getRed()   - c2.getRed());
                                    int diffGreen = Math.abs(c1.getGreen() - c2.getGreen());
                                    int diffBlue  = Math.abs(c1.getBlue()  - c2.getBlue());
                                    Those values you can just divide by the amount of difference saturations (255), and you will get the difference between the two.
                                    
                                    float pctDiffRed   = (float)diffRed   / 255;
                                    float pctDiffGreen = (float)diffGreen / 255;
                                    float pctDiffBlue   = (float)diffBlue  / 255;
                                    After which you can just find the average color difference in percentage.
                                    
                                    (pctDiffRed + pctDiffGreen + pctDiffBlue) / 3 * 100
                                    
                                    1 Reply Last reply
                                    0
                                    • K Offline
                                      K Offline
                                      KzR69100
                                      wrote on last edited by KzR69100
                                      #22

                                      That is very complicated.

                                      QMap is a kind of database, a dictionary that is right ?

                                      I will try with you'r first code :

                                      I don't understand the function void Test.

                                      I got a function who return me a color in QRGB, it named "color" can i say just :

                                      If (color == (the color in the Qmap (i dont know how to call it))){
                                      cost = 50;
                                      image = (red image)
                                      }
                                      ?

                                      The problem is that i understand QMap, but i don't know how to use it and i'm new to programmation

                                      mrjjM 1 Reply Last reply
                                      0
                                      • K KzR69100

                                        That is very complicated.

                                        QMap is a kind of database, a dictionary that is right ?

                                        I will try with you'r first code :

                                        I don't understand the function void Test.

                                        I got a function who return me a color in QRGB, it named "color" can i say just :

                                        If (color == (the color in the Qmap (i dont know how to call it))){
                                        cost = 50;
                                        image = (red image)
                                        }
                                        ?

                                        The problem is that i understand QMap, but i don't know how to use it and i'm new to programmation

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

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

                                        • That is very complicated.
                                          Ok, its plain c++ and maps are often used. I didnt mean to make it
                                          complicated. Using say a DataBase would be 10 times the code.

                                        QMap is a kind of database, a dictionary that is right ?
                                        Yes. You tell it ( whatkey, whatvalue)
                                        and then u can ask for it to return the value for a key.
                                        In this sample we used a color as key.

                                        I don't understand the function void Test.

                                        void Test() {
                                          // this is the color to look for
                                          QColor col = QColor(100, 100, 100); // getColor()..
                                         // check the map if it has the color ( suing contains)
                                          if (Costs.contains( col.rgb() )) {
                                            CostInfo& ci = Costs[col.rgb()]; // take the struct from list
                                            qDebug() << "found:" << "cost:" << ci.Cost << "img:" << ci.ImageName;
                                          }
                                        }
                                        
                                        > 
                                        > I got a function who return me a color in QRGB, it named "color" can i say just :
                                        > If (color == (the color in the Qmap (i dont know how to call it))){
                                        You cannot use == you must use Contains function.
                                        if (Costs.contains( color.rgb() )   ) {
                                            CostInfo& ci = Costs[color.rgb()]; // take the struct from list
                                           QColor TheColor(i.key()); // this is the base color
                                           int Cost=ci.Cost; // use the values.
                                          }
                                        
                                        1 Reply Last reply
                                        0
                                        • K Offline
                                          K Offline
                                          KzR69100
                                          wrote on last edited by KzR69100
                                          #24
                                          void Test() {
                                            // this is the color to look for
                                            QColor col = QColor(100, 100, 100); // getColor()..
                                           // check the map if it has the color ( suing contains)
                                            if (Costs.contains( col.rgb() )) {
                                              CostInfo& ci = Costs[col.rgb()]; // take the struct from list
                                              qDebug() << "found:" << "cost:" << ci.Cost << "img:" << ci.ImageName;
                                            }
                                          }
                                          

                                          So the QColor col is my returned color that is right ?

                                          i don't understand the "CostInfo& ci"

                                          And do the size of the picture in the Qmap have to be resized?

                                          PS :
                                          my function is :

                                          for(int x = topLeft.x(); x < maxX; ++x) {
                                          		for(int y = topLeft.y(); y < maxY; ++y) {
                                          			image.setPixelColor(x, y, colour);
                                                                                        if (Costs.contains( colour.rgb() )) {          // it will be that ?
                                                                                             CostInfo& ci = Costs[colour.rgb()];
                                          		}
                                          	}
                                          }
                                          

                                          to have all RGB in my struct can i use a "for" (sorry to always say "can i use a for") but the problem is that i want all RGB value with my 15 - 20 pictures

                                          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