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

Pyqt GUI, Lineedit, Game MasterMind
Increase font size on target board using Qt for Embedded Linux
How do you write a failing Qt Test that generates BFAILs and BPASSes?
Get local time in seconds using Qt
QML TextView change scroll-bar style?
expand qgraphicsrectitem to cover complete scene and expand all children's in same proportion
QML: Cannot read property 'xxx' of undefined
Trouble of mutating QList which is actually QVariant without copying it
How to set the position of QMainWindow?
QAudioRecorder detect user not speaking and stop
BB10 Cascades ListView is jittering / shaking
Drop caps in Qt rich text
Transmit QMap by QMimeData
Zoom feature for QWebEngine does not work
How to prevent QWebEngineView to grab focus on setHtml(…) and load(…) calls?
How to use QPainter in QOpenGlWidget's paintGL

Categories

HOME
user-interface
keyboard
tomcat
barcode-scanner
swi-prolog
systemd
filesize
bing-search
dji-sdk
ipv6
capistrano
fedora
onvif
native-base
phpseclib
stack-overflow
orc
jodatime
pywinauto
fosuserbundle
raml
social-tables
qa
sipp
web-push
plyr
unrar
android-cardview
tf-idf
guice
intune
matlab-app-designer
objectmapper
cloveretl
devtools
polyml
netstat
installanywhere
oauth2
stat
get-event-store
notesview
virtual-memory
realstudio
icecast
iptv
android-sharing
dex
rcharts
maatwebsite-excel
s-function
bilinear-interpolation
seccomp
qtcpsocket
eclemma
qpixmap
verisign
wikimedia-commons
odesk
ruby-on-rails-4.1
astropy
bjam
unhandled
gql
wlanapi
replicaset
whitespace-language
subfolder
fasterxml
stripe.net
pageviews
meteorite
strdup
sensormanager
infinite
yii-booster
jacob
oembed
system-requirements
lynx
android-dialog
jdom
dropshadow
mmc3
delphi-prism
datacontracts
kernel32
grails-validation
jmenu
ognl
turbine
dm
projectpier
acceleration
remote-working

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