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

Get focus (or tab) order
Error getting DBus Interface property with QDBusInterface
header view when QFileSystemModel is displayed in a QTableView
Qt cannot execute existing remote file: No such file or directory
QTreeWidget and QFileSystemModel
obfuscate password in qt
dynamically add data to QTableView
Changing qml application language on runtime depending user select
QObject::connect in QRunnable - console
Size of QByteArray is hard to compute?
QT with Visual Studio 2013 Express
Can we use custom qbject-derived classes as types in javascript?
Qt is successfully creating a file but not writing to it with QTextStream
Data send by Debugging
Qt QNetworkAccessManager with SSL
Parse *.vcproj file in Qt

Categories

HOME
yii
activex
hid
ios-simulator
cryptography
greasemonkey
drag-and-drop
cocotb
msmq
azure-graph-api
xamarin-zebble
parsley.js
game-maker
tiff
xbap
trace32
pygobject
vertica
hidden
exec-maven-plugin
pe
animated-gif
pdf.js
sap-lumira
tortoisehg
annotation-processing
dnsmasq
intune
mongodb-3.4
fotorama
xmlunit
nest
gnu-classpath
testlink
ms-dos
oauth2-playground
access-denied
httphandler
virtual-memory
julius-speech
destroy
sony-future-lab-n
md5-file
maven-jaxb2-plugin
exc-bad-access
scala-collections
d3v4
bpms
e
bilinear-interpolation
conan
android-sdcard
declarative
bbc-micro
timestamping
gulp-uglify
preferenceactivity
wif4.5
wikimedia-commons
ibm-was
nastran
scala-swing
nssortdescriptor
subview
maven-shade-plugin
rebar
xulrunner
gpu-programming
jms-serializer
typesafe-stack
meteorite
compiler-flags
visual-c++-2005
insertion
oscilloscope
jquery-cycle
2d-vector
handshake
arrow
websphere-6.1
server-name
firebird1.5
flymake
acpi
n-queens
drawimage
winsxs
jquery-1.4
digiflow
webresponse
anonymous-types
aptitude
mygeneration
chunking
getimagesize
3270
eai
account-management

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile