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

Qt Gesture Grabbing only Pinch works reliably
How can I pass a QML object reference into Qt C++?
Font property not working on QListView item sub-control
Injecting widgets depending on item selected in drop down
QTextStream readAll() removes Newlines
How do you multiply a QString, so it repeats itself n times?
How to suppress unicode characters in QString or convert to latin1
The second addMapping() can not work using QDataWidgetMapper
How to delete content from QPixMap
Creating Window That Has No Close Button in Qt
How To Insert Data which is generated From Qt App into MySQL Database through PHP?
Sqlite for client server program
How to get NavigationKey.up value in qml and c++?
Qt and Boost (installed through Homebrew) result in Qt compile errors
How record Qt Widgets StyleSheet in a sepatated file
How to get a activeFocusItem property in qml from main.cpp?

Categories

HOME
visual-studio
mc
freeradius
yii
schema.org
d3.js
redmine
operating-system
devexpress
compilation
activemq
braintree
biztalk
flurry
azure-data-lake
visualforce
pyresttest
anchor
twitter-oauth
match
cuba-platform
async-await
tomcat8
checkout
libgit2
kamailio
mime-types
pdf.js
pubxml
ipa
beego
d3.js-v4
steam-web-api
nsmutablearray
plantuml
listjs
serversocket
rails-postgresql
spinner
elastica
django-smart-selects
hdiv
ejml
flipkart
pyttsx
julius-speech
apache-toree
intellij-lombok-plugin
swift2.3
datediff
instafeedjs
nrf51
data-management
p6spy
powerpoint-2013
codecharge
uibinder
apple-configurator
jeasyui
miniconda
whitespace-language
findersync
window-resize
treeline
network-printers
csslint
redpitaya
zend-search-lucene
air-native-extension
usb-flash-drive
mod-perl
hyphen
mousemotionlistener
acitree
web-frameworks
arel
proc-open
oembed
user-forums
towers-of-hanoi
struts2-json-plugin
audiotoolbox
prefuse
dropshadow
uploading
gwt-celltable
pyjamas
downgrade
ixmldomdocument
3270
external-sorting
.net-client-profile
ntruencrypt
efs
objectinstantiation
activex-exe
gui-designer
jvm-bytecode

Resources

Encrypt Message