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 can I get a QFileDialog to prompt for overwrite?
bad_allock error in trying to read large file in qt
Pyqt Session management error
Qt - Can not show a Qt.Tool window after parent is shown?
Qt Overloaded Mouse Event Handler not being called when mouse is clicked
Qt Quick vs. Qt Widget
Using libsndfile to play sound real time c++ in qt
Widget promoted to a custom widget in QtDesigner shows up blank
Qt: receive keyboard events when my application doesn't have focus
QT, including a .dll using QTEditor
Qt. Fastest way to draw 1024x1024 dots on screen
QCA blowfish decryption fails
Qt OpenGL GUI: Add and Undo Qpushbutton
In Qt Designer, can you hide a QDockWidget by default
MessageBox in qt and custom icon on a frame
Qt slot and class inheritation

Categories

HOME
entity-framework
apache-nifi
blast
seedstack
opencv-python
compilation
out-of-memory
vsts-build
informatica
docker-cloud
nsis
mirc
lvm
mosquitto
linker
scheduler
specifications
csrf
documentum
vimeo
angular4
bundler
l20n
office-ui-fabric
home
dashdb
bsd
sharedpreferences
pymongo
intel
ini
chronicle-queue
nurbs
web-push
plyr
strophe.js
pyspark-sql
bonita
unmarshalling
openbr
envoy
source-insight
john-the-ripper
des
netstat
variable-variables
telephonymanager
hypothesis-test
apache-toree
swift2.3
windows-media-player
rm
android-5.0-lollipop
ol3-google-maps
gapi
msg
frame-grab
jeasyui
mnesia
openmrs
android-expansion-files
myfaces
odesk
cryptojs
apache-commons-digester
bjam
gluon-desktop
embeddedwebserver
componentart
wicked-gem
innerhtml
password-recovery
screwturn
.htpasswd
pageviews
wso2bam
ivalueconverter
progress-db
sql-view
indesign-server
flv
eager-loading
rikulo
html-frames
xmlwriter
multiscaleimage
cgrectmake
gitx
viewpagerindicator
taskkill
pageheap
cakeyframeanimation
prefuse
trialware
platform-independent
drawimage
groovy-console
snackjs
net-use
page-curl
grails-validation
tessellation
android-input-method
browser-based
html-components
source-code-protection
iphone-sdk-3.1.3
synthesizer

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