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

Animation on QML not showing?
Qt Hovering over a button and triggering a function?
Component with code-controlled properties (without default value)
Qt 5.7 QDockWidget default size
Offset rotation for QML Item
CUDA NPP GaussFilter corrupts images
How to set default directory of QFileDialog on Windows WinRT
QGridLayout: You must insert this grid into another layout
Make QToolBar transparent
Running .bat file show Error findstr not recognized from QProcess Qt
Qt - Extracting words with the first letter in uppercase from a QString
Finding the first occurence of any of several strings in a `QByteArray`
Write/Read bit fields structure to/from file
QCombobox background color for edit part of combobox
how to add a new c++ class that inherits from QThread in a Qt widget application
Qt Creator on Fedora 25: “No valid kits found”

Categories

HOME
opengl
powerbi
macos-sierra
blast
mpdf
dry
plaid
ews
consul
elk-stack
tfs2010
gtk
log4j2
hystrix
android-externalstorage
spring-amqp
pushdown-automaton
asp.net-core-1.0
phonegap-build
cocos2d-android
jaspersoft-studio
amazon-mws
akka-persistence
android-permissions
quantitative-finance
ava
quartz.net
jpa-2.1
adapter
cube
sfml
ibm-connections
microprocessors
splunk-query
docx4j
matlab-app-designer
vcenter
progid
reporting
microsoft-certifications
adminer
apptentive
stackpanel
installanywhere
recovery
oauth2
system-on-chip
secure-coding
distributed-caching
raytracing
robust
1wire
fluid-mac-app-engine
nuclio
music21
rm
nothing
brightscript
sonatype
network-protocols
dex
swiperefreshlayout
cognos-tm1
d3v4
cefpython
rational-performance-test
svnserve
always-on-top
yii2-model
adp
exists
mnesia
vim-plugin
m4
html.actionlink
largenumber
libz
supersocket.net
replicaset
cakephp-2.2
wicket-1.5
window-resize
dmake
ecos
git-reset
hyperloglog
refit
bittorrent-sync
meteor-collections
jomsocial
spring-social-linkedin
meteorite
particle-swarm
strcmp
insertonsubmit
aero
oscilloscope
itunes-sdk
icenium
email-verification
daap
getproperty
border-box
telerik-ajax
jzmq
kobold2d
differentiation
server-variables
qt-mobility
kext
kernel32
cewolf
airprint
explicit
eventaggregator
levels

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