LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-17-2004, 09:39 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
Combine number and text


I have a this code right now:
Code:
QLabel *label = new QLabel("text", this, "name");
The function QLabel takes three arguments, the first is the actual text, the second is the parent widget and the last (the interesting) is the name of the label (not the name of the variable).

The question is really easy (I think). I don't know how many of these I will need, so I would get the number of labels at runtime and then I would like to call them "name1", "name2", etc by just using a for-loop, that would produce like:

Code:
for (int i = 0; i<num; i++)
  QLabel ? = new QLabel("text", this, ?);

produces

QLabel *label1 = new QLabel("text", this, "name1");
QLabel *label2 = new QLabel("text", this, "name2");
QLabel *label3 = new QLabel("text", this, "name3");
...
QLabel *labeln = new QLabel("text", this, "namen");
How can I do this, combine both the string "text" with the integer i in the argument, and also combine the text "label" with the same number in the name of the variable.
 
Old 11-17-2004, 10:02 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
A simple solution would be to have a function that returns the next label name.

Untested code:
Code:
const std::string& next_label = get_next_label_name();

QLabel *label = new QLabel("text", this, next_label.c_str());
Where get_next_label_name() is defined as:
Code:
std::string get_next_label_name()
{
   static int count = 1;
   std::ostringstream oss;

   oss << "Name " << count++;

   return oss.str();
}
 
Old 11-17-2004, 10:46 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
I still have the problem with the variable-name. This one is really important. Is there even a solution to this? Can I create like three variables called var1, var2, var3 with a for-loop, without using arrays.
 
Old 11-17-2004, 11:13 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
You simply cannot do it that way.
You can, however, use an array or a QList of pointers to QLabel's.
An array would look something like this:
Code:
QLabel *labels[100];

for (int i = 0; i < 100; ++i) {
    std::ostringstream oss;
    oss << "Name " << i;

    labels[i] = new QLabel("text", this, oss.str());
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenOffice Calc Text to Number pokergeek Linux - Software 1 03-29-2012 10:13 AM
Combine text to forms image carstenson Linux - Software 0 12-15-2004 04:56 PM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 03:03 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM
I need a program to combine text with gif files stephenoregan4 Linux - Software 1 02-16-2004 09:29 AM


All times are GMT -5. The time now is 01:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration