Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-17-2004, 09:39 AM   #1
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:
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:

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


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
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:
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:
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
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
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
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:
QLabel *labels[100];

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

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


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 AM.

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