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

Get notified when QQuickItem will need updating
Qwt date-time axis
QDateEdit: months popup is covered by days on OSX
How to install a missing Qt module?
QtGstreamer on windows
Why is the Qt event loop left
why qwtplot3d don't work in QT5.7.0 ? But it works well in QT5.5.0
is there an easy way to make codes in qt creator(minGW) work in VS2015(msvc)?
How to de-saturate a color defined by RGB in declarative QML
Qt replace variables in resources
When exactly are Qt Quick items repainted?
Suspending the third-party QEventLoops
Check if QVariant is convertible to other QVariant type
QPdfwriter: how to select an editable font for Adobe Acrobat?
QProcess send input to other application (Remote Control Example)
How to pass objects between qml

Categories

HOME
qt
jboss
barcode-scanner
haproxy
code-formatting
ews
jersey-2.0
nsis
dependencies
google-sheets-query
tibco
phpmqtt
telegraf
yahoo
azure-graph-api
smooks
xbox360
bnf
checkout
azure-machine-learning
tableview
sqlplus
multi-upload
device
react-dnd
fusetools
points
math.js
iwebbrowser2
adapter
sfml
javax.persistence
splunk-query
premake
sca
grails-2.5
dreamfactory
bluemixtools
love2d
identify
uivisualeffectview
elastica
export-to-pdf
resampling
angular-datatables
bit-shift
windows-media-player
deployd
nothing
cordova-ios
sequence-diagram
android-mediarecorder
ptvs
rcharts
google-shopping-api
petsc
multi-touch
mathml
bbc-micro
jeasyui
utf-16
vim-plugin
wif4.5
cortana
ajax4jsf
nastran
css-paged-media
replicaset
sequence-sql
alwayson
fuseesb
scala-swing
text-align
fiware-bosun
comctl32
iodocs
windows-rt
android-2.3-gingerbread
hyphen
vlab
acitree
ember-table
axacropdf
oembed
examine
ntlmv2
django-filebrowser
itunes-sdk
xmlwriter
popup-blocker
monocross
algol68
mmc3
expression-web
motodev-studio
haiku
botnet
page-curl
cewolf
simile
asp.net-mvc-views
aptitude
active-record-query
hungarian-notation
mygeneration
solandra
cassini
j2mepolish
fgetc
asdoc
.net-client-profile
main-method
rakudo
server-load

Resources

Encrypt Message