qt


Create multiple QwtPlotCurve dynamically


I have a QStringList (m_NameList) which contains 4 elements in it. For each string, I create a QwtPlotCurve (QMap < QString, QwtPlotCurve*> m_NamePlotC;) and set different Pens for each plotCurve:
for(unsigned int i= 0; i< m_NameList.count(); ++i)
{
QwtText title(m_NameList.at(i));
title.setFont(fontNormal);
m_NamePlotC[m_NameList.at(i)]= new QwtPlotCurve();
m_NamePlotC[m_NameList.at(i)]->setTitle(title);
if(i== 0)
{
m_NamePlotC[m_NameList.at(i)]->setPen(QPen(Qt::green, 1, Qt::DashLine));
}
else if(i== 1)
{
m_NamePlotC[m_NameList.at(i)]->setPen(QPen(Qt::blue, 1, Qt::DashLine));
}
else if(i== 2)
{
m_NamePlotC[m_NameList.at(i)]->setPen(QPen(Qt::cyan, 1, Qt::DashLine));
}
else if(i== 3)
{
m_NamePlotC[m_NameList.at(i)]->setPen(QPen(Qt::yellow, 1, Qt::DashLine));
}
m_NamePlotC[m_NameList.at(i)]->setRenderHint(QwtPlotItem::RenderAntialiased, true);
m_NamePlotC[m_NameList.at(i)]->setCurveAttribute(QwtPlotCurve::Fitted);
}
My problem is with the getting rid of if-statements. Is there any creative way that I set pens dynamically based on the number of QStringList elements with different color for each element in my QStringList, without using ifs?
Thank you!
It seems that in each line, the only thing changing is the colour. You can maintain a list of colours, let's call it coloursList and then replace the ifs by
m_NamePlotC[m_NameList.at(i)]->setPen(QPen(coloursList[i], 1, Qt::DashLine));
I'm not sure which language you have used, so my indexing syntax may be a bit off, but I hope the logic is clear.

Related Links

Embedded Qt GraphicsScene coordinates
How to forward signals to the last-clicked QGraphicsScene
Understand when qthread finish
QtMobility Several QMediaplyers in one applications
Group QRadioButtons in rows from a QTableWidget
QAbstactTableModel insert at top
What's the right way to apply stylesheet on a class derived from QPushButton?
can’t debug in qt sdk 1.2-during startup program exited with code 0x0
How to map a combobox with static items to database field?
Is it possible to emit a signal from the baseclass of a derived object using “this”
iter with foreach
QRadioButton Array issue
Error during compiling the Qt libraries
CMake CPack debian packages
DELETE with qsqlquery
QTableView - sort headers

Categories

HOME
android-studio
memory
windows-8.1
clearcase
focus
adfs3.0
compilation
braintree
openlayers-3
ontology
facebook-android-sdk
clone
angular-meteor
hystrix
driver
amazon-emr
csrf
bundler
cuba-platform
supervisord
nose
psql
adminlte
data-type-conversion
dashdb
web-analytics
pymongo
perfino
cellular-network
high-availability
spreadsheetgear
onchange
sendmessage
sparkle
monitor
python-2.x
universal
char-pointer
express-session
xerces-c
orchardcms-1.8
subclassing
text-decorations
microkernel
ejml
pyttsx
expandablelistview
asihttprequest
xcode8.2
dredd
sonarqube5.2
roracle
xcglogger
maven-jaxb2-plugin
mobile-angular-ui
finder
mifos
tablespace
openquery
jsf-2.3
tsqlt
maven-release-plugin
adp
urbit
nastran
angular-ui-select
lumia-imaging-sdk
accessory
two.js
incognito-mode
tabletools
xmi
roxy-fileman
django-settings
mute
microformats
examine
office-app
smartfox
xcode4.5
wndproc
image-formats
kobold2d
background-repeat
rtd
django-pagination
hungarian-notation
solandra
explicit
ccnet-config
ixmldomdocument
rawcontacts
downloadfile

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App