Results 1 to 10 of 10

Thread: How to set colors in arraylist?

  1. #1
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Post How to set colors in arraylist?

    Hi All,
    I am new to this forum and QT, i am in need of setting color for every value which is set as string and stored in array.
    I need to set for every line a color so can any one help me please
    This is my sample code!!!

    void mainwindow::content(QList<QString> placename,QList<QString> address,QList<QString> username,QList<QString> time)
    {
    QStringList strList;

    for(int i=0 ; i<placename.count(); i++)

    {

    QString name = username[i];
    QString place = placename[];
    QString profileaddress = address[i];
    QString Time = time[i];

    strList << name + "\n" + place + "\n" + profileaddress+ "\n" + Time;
    }


    For Name i need one color,place i need a different color, like that for other too!!
    Kindly help me please

    Thanks in Advance and Regards,
    Naufal.A

  2. #2
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to set colors in arraylist?

    A QString doesn't have a color. You can color it, if you write the lines to prompt.
    Or create a class ColorString...

  3. #3
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set colors in arraylist?

    Hi Qiieha,
    Can i have some example coding or functions based on Qcolorstring??

    Thanks in advance and Regards,
    Naufal.A

  4. #4
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set colors in arraylist?

    Hi,
    I think you must create colormap for your problem. You can search about colormap algorithm like jet, gray, hot, winter, etc. This my simple code to create winter colormap :
    Qt Code:
    1. for (int i = 0; i < m_colorMapLength; i++)
    2. {
    3. spring[i] = 1.0f * i / (m_colorMapLength - 1);
    4. cmap[i][0] = m_alphaValue;
    5. cmap[i][1] = 255;
    6. cmap[i][2] = (int)(255 * spring[i]);
    7. cmap[i][3] = 255 - cmap[i][1];
    8. }
    To copy to clipboard, switch view to plain text mode 
    m_colorMapLength is your listarray length. cmap variable is ARGB value. From this algorithm, you can set different color at your listarray.
    Are you confused with my explanation?

    Thank you for your attention.

    Best regards,

    Myta

  5. #5
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set colors in arraylist?

    Hi myta212,
    I am little bit confused with your answer!!! I will try that and tell you in sometime.. Thanks for your response
    I will check out and tell you soon .,

    Thanks in advance and Regards,
    Naufal.A

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to set colors in arraylist?

    You can use QPair.
    Qt Code:
    1. QList<QPair<QString, QColor> > myList;
    2. myList << qMakePair( name, QColor(255, 0, 0) );
    3. myList << qMakePair( place, QColor(0, 255, 0) );
    4. myList << qMakePair( profileaddress, QColor(0, 0, 255) );
    5. myList << qMakePair( Time, QColor(255, 255, 0) );
    To copy to clipboard, switch view to plain text mode 
    Or something smarter like a struct or class as you'll probably will need to do much more with those rather than just print them (keeping them all in a list isn't too smart).

  7. #7
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set colors in arraylist?

    Quote Originally Posted by Spitfire View Post
    You can use QPair.
    Qt Code:
    1. QList<QPair<QString, QColor> > myList;
    2. myList << qMakePair( name, QColor(255, 0, 0) );
    3. myList << qMakePair( place, QColor(0, 255, 0) );
    4. myList << qMakePair( profileaddress, QColor(0, 0, 255) );
    5. myList << qMakePair( Time, QColor(255, 255, 0) );
    To copy to clipboard, switch view to plain text mode 
    Or something smarter like a struct or class as you'll probably will need to do much more with those rather than just print them (keeping them all in a list isn't too smart).

    Hi,
    I am not using qml for design and i am using UI for designing,this qmake shows error , is there any header to be included for it???
    I got an idea i will try too!!! But,help me if you know the solution

    Thanks in advance and Regards,
    Naufal.A

  8. #8
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to set colors in arraylist?

    What error does it show?

    Here are includes you need to make the above code compile:
    Qt Code:
    1. #include <QList>
    2. #include <QPair>
    3. #include <QColor>
    To copy to clipboard, switch view to plain text mode 
    When using new object go to documentation to find out what you need to include, but most of the time the file name is the same as the object name.

  9. #9
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set colors in arraylist?

    Hi spitfire,
    I am very sorry that,the code is running but there is no color change in my listwidget, its same as before.
    I have a doubt where you have declare myList,but in my coding i have declared as strList but when i change it shows error.
    Am i doing the right thing, i have added the header and then i kept the same code given by you,then too i did not get change in color.
    Sorry for troubling you,tell me what is the problem or mistake done by me??

    Thanks in advance and Regards,
    Naufal.A

  10. #10
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to set colors in arraylist?

    Ok, I see you're not too familiar with what you're doing.

    I assume that you have a QListWidget in which you want to display a number of strings, each string in different color.

    Here's an example how it can be done (I've replaced QPair with structure):
    Qt Code:
    1. // MainWindow.h
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QtGui/QMainWindow>
    6.  
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. struct ColoredString // Struct that stores string and it's color for easy use
    14. {
    15. // constructor that takes string and color for easy single-line assignment
    16. ColoredString( const QString& str, const QColor& clr ) : value( str ), color( clr ) {}
    17. QString value;
    18. QColor color;
    19. };
    20.  
    21. MainWindow(QWidget *parent = 0);
    22. ~MainWindow();
    23.  
    24. void prepareData( void ); // example how to prepare a list of colored strings
    25. void showData( void ); // example how to add colored strings to QListWidget
    26.  
    27. private:
    28. QListWidget* listWidget;
    29. QList< ColoredString > coloredStrings;
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //mainwindow.cpp
    2. #include "mainwindow.h"
    3.  
    4. #include <QListWidget>
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QMainWindow(parent),
    8. listWidget( new QListWidget( this ) ),
    9. coloredStrings( QList< ColoredString >() )
    10. {
    11. this->setCentralWidget( this->listWidget );
    12.  
    13. this->prepareData();
    14. this->showData();
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19.  
    20. }
    21.  
    22. void MainWindow::prepareData( void )
    23. {
    24. this->coloredStrings << ColoredString( "Line 0", QColor( 0, 0, 0 ) );
    25. this->coloredStrings << ColoredString( "Line 1", QColor( 255, 0, 0 ) );
    26. this->coloredStrings << ColoredString( "Line 2", QColor( 0, 255, 0 ) );
    27. this->coloredStrings << ColoredString( "Line 3", QColor( 0, 0, 255 ) );
    28. this->coloredStrings << ColoredString( "Line 4", QColor( 255, 255, 0 ) );
    29. this->coloredStrings << ColoredString( "Line 5", QColor( 255, 0, 255 ) );
    30. }
    31.  
    32. void MainWindow::showData( void )
    33. {
    34. // loop through your items to add them to the widget
    35. for( int i = 0; i < this->coloredStrings.size(); ++i )
    36. {
    37. // create QListWidgetItem for your widget
    38. QListWidgetItem* item = new QListWidgetItem( this->listWidget );
    39. // set text on the item
    40. item->setText( this->coloredStrings[i].value );
    41. // set color for the text
    42. item->setTextColor( this->coloredStrings[i].color );
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 
    I hope that's all you need to implement whatever you want.

    Also refereing to your sample code in first post: wouldn't it be simpler to create "User" class/struct that holds all the elements and pass around a list of those elements rather than four separate lists for each element?

Similar Threads

  1. [java] HasSet vs ArrayList and modelling issue
    By mickey in forum General Programming
    Replies: 2
    Last Post: 13th September 2010, 18:14
  2. Max and Min Values for qint64
    By gmat4321 in forum Qt Programming
    Replies: 3
    Last Post: 24th August 2010, 09:20
  3. concatenate two values
    By rk0747 in forum Qt Programming
    Replies: 4
    Last Post: 30th May 2010, 10:43
  4. ArrayList-Like Qt Class
    By zdunham in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 15:29
  5. How can I create button from arraylist.
    By electronicboy in forum Qt Programming
    Replies: 5
    Last Post: 5th October 2009, 20:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.