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

How to display a QMessageBox on top of all windows
How can I install Qt 5.2.1 from the command line in Cygwin?
QMdiArea in TabbedView glitch on tab switching
How can I follow OOP base concepts with QObject derived class instance?
Qt 5.3. Painting on QOpenGLFrameBuffer
Using QNetworkAccessManager::authenticationRequired with own input widget / asynchronously
QTCreator 3.1 design tab not working
Qt's installer file gives “… is malicious, and Chrome has blocked it” message in Chrome [closed]
How to add style via setStyleSheet() without losing orignal style in Qt?
Convert QUrl with percent encoding into string
Qt ItemDelegate with a tool button: can't click
QWebView: linkClicked doesn't get emitted with certain page (javascript?)
How to update the QT Mainwindow at each loop in mainwindow.cpp
QGraphicsItemGroup to OpenGL texture
Deploy QML application with localstorage plugin
After by hiding its vertical header, can I let user resize the row of QTableWidget?

Categories

HOME
hpoo
amazon-web-services
xpath
webview
magento2
powerbi
celery
consul
mosquitto
hyperledger
save
u-boot
angular2-template
cocotb
wine
lc3
fasm
business-intelligence
android-externalstorage
ex
bundler
dhcp
complexity-theory
nest-api
fosuserbundle
weex
backtracking
qa
websphere-portal
raima
unordered-multimap
onchange
d3.js-v4
toad
winsock
progid
series
om-next
cloudera-manager
image-optimization
amazon-kms
jfxtras
spinner
code-rally
extbase
selenide
praat
imgur
jacoco-maven-plugin
minikube
audio-converter
intellij-lombok-plugin
redisson
scala-collections
emacs25
ogg
nclam
svnserve
ftp-client
jtag
dojo-build
bbc-micro
node.js-connect
static-methods
node-glob
android-async-http
flipclock
enunciate
opencyc
srv-record
search-form
ampersand
wicked-gem
tree-structure
fuseesb
sunos
juttle
dc
mobile-country-code
quantify
webautomation
preorder
boolean-algebra
web-frameworks
donut-chart
session-0-isolation
ip-geolocation
msinfo32
pcspim
drawimage
getopts
kernel32
dip
3-tier
sscli
urchin
tabbarcontroller
j2mepolish
motif
community-server

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App