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: How to close multiple windows of the same widget?
QML ListView current item not changing with keystrokes or mouse
Qt How to copy 2. and 3. index of char array to quint16?
Displaying live camera image in Qt GUI
Painting a QGraphicsItem based on the QGraphicsView it is displayed in
QFileDialog component signals
Combobox selection warns “This implementation does not support subelements”
Systray in Ubuntu 13.10
PySide - Click an item inside of a QTreeWidget with QTest
Qt: QGridLayout refusing to add widgets
Qt creator debug mode has disappeared
How to install QtQuick 1.1 on QT 5.1.1
Serialized Data: How to Check if byte array is Qt or Boost
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

Categories

HOME
hpoo
oracle
magento2
memory
list
exchange-server
bots
add-on
mongoid
moodle-api
drag-and-drop
impala
firebase-database
twitter-oauth
csrf
rangy
uima
pickle
checkout
adobe-illustrator
home
wsf
android-custom-view
web-analytics
gollum-wiki
jpa-2.1
cellular-network
jackrabbit-oak
python-imageio
fatfs
dnsmasq
fusion
source-insight
actframework
caesar-cipher
busboy
keyboard-layout
stackpanel
jslint
forms-authentication
recovery
cloudera-sentry
yoast
spooler
jqgrid-asp.net
xcode8.2
wysihtml5
roracle
md5-file
password-hash
karabiner
maven-jaxb2-plugin
exc-bad-access
gtk#
google-news
sesame
ogg
controllers
decoder
xcode8-beta4
powerpoint-2013
gnucash
adjacency-list
fdt
mnesia
teamviewer
trust
grinder
android-studio-import
postal-code
modern.ie
signed
subview
hp-idol-ondemand
symja
rubber
cout
berkeley-db-xml
network-printers
stripe.net
property-injection
meteorite
xs
rikulo
session-0-isolation
wndproc
server-name
struts2-s2hibernate
kobold2d
glassfish-esb
qt-mobility
grails-validation
active-record-query
explicit
icicles
getimagesize
jquery-effects
iphone-sdk-3.1.3
watchpoint
gui-designer
parentid

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile