PDA

View Full Version : How to set colors in arraylist?



naufalahad
28th November 2011, 07:00
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

Qiieha
28th November 2011, 09:27
A QString doesn't have a color. You can color it, if you write the lines to prompt.
Or create a class ColorString...

naufalahad
28th November 2011, 09:32
Hi Qiieha,
Can i have some example coding or functions based on Qcolorstring??

Thanks in advance and Regards,
Naufal.A

myta212
28th November 2011, 09:37
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 :

for (int i = 0; i < m_colorMapLength; i++)
{
spring[i] = 1.0f * i / (m_colorMapLength - 1);
cmap[i][0] = m_alphaValue;
cmap[i][1] = 255;
cmap[i][2] = (int)(255 * spring[i]);
cmap[i][3] = 255 - cmap[i][1];
}
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

naufalahad
28th November 2011, 09:44
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

Spitfire
28th November 2011, 09:57
You can use QPair.

QList<QPair<QString, QColor> > myList;
myList << qMakePair( name, QColor(255, 0, 0) );
myList << qMakePair( place, QColor(0, 255, 0) );
myList << qMakePair( profileaddress, QColor(0, 0, 255) );
myList << qMakePair( Time, QColor(255, 255, 0) );
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).

naufalahad
28th November 2011, 10:03
You can use QPair.

QList<QPair<QString, QColor> > myList;
myList << qMakePair( name, QColor(255, 0, 0) );
myList << qMakePair( place, QColor(0, 255, 0) );
myList << qMakePair( profileaddress, QColor(0, 0, 255) );
myList << qMakePair( Time, QColor(255, 255, 0) );
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

Spitfire
28th November 2011, 11:44
What error does it show?

Here are includes you need to make the above code compile:
#include <QList>
#include <QPair>
#include <QColor>
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.

naufalahad
28th November 2011, 12:12
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

Spitfire
28th November 2011, 14:56
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):
// MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

class QListWidget;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
struct ColoredString // Struct that stores string and it's color for easy use
{
// constructor that takes string and color for easy single-line assignment
ColoredString( const QString& str, const QColor& clr ) : value( str ), color( clr ) {}
QString value;
QColor color;
};

MainWindow(QWidget *parent = 0);
~MainWindow();

void prepareData( void ); // example how to prepare a list of colored strings
void showData( void ); // example how to add colored strings to QListWidget

private:
QListWidget* listWidget;
QList< ColoredString > coloredStrings;
};

#endif // MAINWINDOW_H

//mainwindow.cpp
#include "mainwindow.h"

#include <QListWidget>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
listWidget( new QListWidget( this ) ),
coloredStrings( QList< ColoredString >() )
{
this->setCentralWidget( this->listWidget );

this->prepareData();
this->showData();
}

MainWindow::~MainWindow()
{

}

void MainWindow::prepareData( void )
{
this->coloredStrings << ColoredString( "Line 0", QColor( 0, 0, 0 ) );
this->coloredStrings << ColoredString( "Line 1", QColor( 255, 0, 0 ) );
this->coloredStrings << ColoredString( "Line 2", QColor( 0, 255, 0 ) );
this->coloredStrings << ColoredString( "Line 3", QColor( 0, 0, 255 ) );
this->coloredStrings << ColoredString( "Line 4", QColor( 255, 255, 0 ) );
this->coloredStrings << ColoredString( "Line 5", QColor( 255, 0, 255 ) );
}

void MainWindow::showData( void )
{
// loop through your items to add them to the widget
for( int i = 0; i < this->coloredStrings.size(); ++i )
{
// create QListWidgetItem for your widget
QListWidgetItem* item = new QListWidgetItem( this->listWidget );
// set text on the item
item->setText( this->coloredStrings[i].value );
// set color for the text
item->setTextColor( this->coloredStrings[i].color );
}
}
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?