// MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/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 ) {}
};
~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:
QList< ColoredString > coloredStrings;
};
#endif // MAINWINDOW_H
// 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
To copy to clipboard, switch view to plain text mode
//mainwindow.cpp
#include "mainwindow.h"
#include <QListWidget>
MainWindow
::MainWindow(QWidget *parent
) 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
// set text on the item
item->setText( this->coloredStrings[i].value );
// set color for the text
item->setTextColor( this->coloredStrings[i].color );
}
}
//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 );
}
}
To copy to clipboard, switch view to plain text mode
I hope that's all you need to implement whatever you want.
Bookmarks