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

What are equivalents for Windows 7 Phone PhoneCallTask, EmailCompmoseTask, and BingMapsTask in Blackberry Cascades
Adjusting QPen thickness when scaling QGraphicsView?
How to scroll QLabel?
error C2976: 'QMap' : too few template arguments in QMetaObject::invokeMethod
QComboBox AbstractItemView::item
Show Text above Gridview in QML
Phonon's VolumeSlider doesn't change the volume
How to simply delete row in QFormLayout programatically
Close the main window and open a new one - PyQt
How to make an oscillation animation in opengl qt
Scrollist issue in QML
C++ Qt - QPixmap does not load with Qt Creator, but load when run via terminal
Transfering values between two forms in Qt
CGAL: Error in compiling code
Clearing textinput in QML
Qt: Qt Designer button panel error

Categories

HOME
mc
eclipse-plugin
braintree
ant
magento-2.0
dronekit-python
apache-kafka-connect
dji-sdk
ipv6
selection
msmq
driver
getopenfilename
sonata
trace32
string-formatting
wsf
bluemix-mobile-services
dragula
opentext
tortoisemerge
password-generator
tracing
unrar
region
openbr
nic
python-2.x
appcode
formsauthenticationticket
hidden-markov-models
right-click
cloudera-sentry
optionaldataexception
julius-speech
xcode8.2
audio-converter
destroy
jenkins-slave
photoswipe
sigsegv
subforms
decoder
nunit-console
wicked-pdf
processor
jeditable
xcopy
mnesia
onepage-checkout
json-spirit
bayesglm
google-maps-api-2
enaml
netdatacontractserializer
unhandled
search-form
libz
phpredis
ocmockito
tkx
jcheckbox
dos2unix
lifetime
webmethod
code39
lsa
rautomation
coalesce
virtual-channel
sunstudio
chartfx
html-frames
jbox2d
oembed
xcode4.5
gitx
live-wallpaper
image-formats
jzmq
java.lang.class
nsfont
kolite
drawimage
startupscript
kext
kohana-auth
cewolf
bll
printqueue
anonymous-types
hungarian-notation
cassini
odac
3270
gui-designer
rootkit

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