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

QGraphicsView - slow scale performance under Linux
Error when using Qt, VC++ and OpenCV library
Qt for Symbian S60 - “Feature not supported”
How to Detect Alt+Enter pressed in QT
Trouble understanding and handling QHeaderView Signals
nube: stdio to QTextedit
How to get title from HTTP text representation via Qt?
QT Align Controls depending upon Screen Resolution
Qt style sheet properties for toolbox buttons
hiding something in response to event (qt)
Qt + OpenGL ES + N900
How to make punch hole in Qt Application
Qt 4.7 QColor constructor: possible bug?
How I can instruct a Qt Creator PRO file to output the *.o files and moc_* files in separate folder?
QTableView added in designer: not showing data
Autocomplete textbox in qt?

Categories

HOME
hpoo
opengl
freeradius
facebook-messenger
barcode-scanner
exchange-server
applescript
concurrency
scroll
devexpress
filesize
histogram
bing-search
gallery
dependencies
firebase-dynamic-links
kurento
spring-security-oauth2
pygobject
c#-7.0
checkout
psql
social-tables
sharedpreferences
altium-designer
multi-upload
openbravo
uilocalnotification
onchange
math.js
php-5.3
npoi
guice
cucumber-junit
sca
custom-keyboard
markov-chains
creation
des
android-n
iscroll
assemblies
export-to-pdf
vb.net-to-c#
expandablelistview
code-push
password-hash
datediff
deedle
mechanicalturk
cognos-tm1
portal
controllers
ipywidgets
pjax
msg
system.web.optimization
django-testing
jsr363
myfaces
grinder
helm
html.actionlink
astropy
mgwt
subview
xml-signature
multivariate-testing
mobile-country-code
angular-ui-select
preorder
usb-flash-drive
assetic
autonumber
apache-shindig
mousemotionlistener
revmob
lru
flv
days
semicolon
android-authenticator
getproperty
msinfo32
autosize
glx
mvcrecaptcha
servercontrol
asp.net-mvc-views
nsconnection
pyjamas
cleartype
powergui
jvm-bytecode

Resources

Encrypt Message