0

I am using Qt and I need some help

  1. How to declare QStringList variable globally in Qt so that I can access it in any function?

  2. How to print all the stuff in QStringList(it contains the file path which it took from QFileDialog) to a lineEdit?

I tried:

ui->lineEdit->setText(filename);

But it gave me error error:QString to non-scalar type QStringList requested. Please give me some examples.

AAEM
  • 1,837
  • 2
  • 18
  • 26
Varun Chitre
  • 3,050
  • 4
  • 23
  • 33

2 Answers2

2

How to declare QStringList variable globally in Qt so that I can access it in any function

Well this isn't a Qt question, but a general C++ one (global variables are frowned upon these days, a more acceptable equivalent is the singleton, search SO for lots of examples). Nonetheless, one way of doing this would be create the QStringList as a static member of the class that instantiates the QFileDialog, the same class will be the one that retrieved it from the dialog anyway and by storing (and returning) it statically you effectively make it global:

class A
{
public:
    void openFileDialog() { // Open the dialog, and store the results in list_. }
    static const QStringList& getFileList() { return list_; }
private:
    static list_;
}

// Just call by:
QStringList list = A::getFileList();

How to print all the stuff in QStringList(it contains the file path which it took from QFileDialog)

Yes, my QStringList contains only 1 string

Well, if your QStringList only contains one string just use:

ui->lineEdit->setText(list_[0]);

Remember a QStringList is derived from QVector< QString >, so you can access the individual QStrings just like any element.

Just to expand your first question, there an infinite number of ways a list of strings can be combined into a single one. But a very common (and easy) method with QStringList is to use join():

QStringList list; list << "This" << "is" << "a" << "list.";
list.join( " " ); // "This is a list. "

I really recommend using the docs, Qt's are brilliant .

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • Thank you for prompt answer here is my modified code QString filename = QFileDialog::getOpenFileName(this,"Open Backup Element","/home","Images(*.img)"); ui->lineEdit->setText(filename[0]); This is giving me error "No matching function for call to 'QLineEdit::setText(QCharRef)' – Varun Chitre Aug 02 '12 at 16:57
  • Because you have changed `filename` to be `QString`, not a `QStringList` like it was originally, it is now indexing each letter of the `QString`. You can now just pass `filename` without the index operator. As Dirk suggested I really recommend reading a beginner C++ book, the problems you are having have nothing to do `Qt`. – cmannett85 Aug 02 '12 at 17:02
  • Oops sorry but I pasted wrong code and wrong error message too :D QStringList filename = QFileDialog::getOpenFileName(this,"Open Backup Element","/home","Images(*.img)"); ui->lineEdit->setText(filename[0]); And error is 'QString' to non-scalar type 'QStringList requested as in OP Well regarding C++ I am not an expert but have basic knowledge which I wish to refine with Qt and then move to other stuff thank you for your help – Varun Chitre Aug 02 '12 at 17:09
  • `QFileDialog::getOpenFileName(..)` returns a `QString`, whilst `QFileDialog::getOpenFileNames(..)` return a `QStringList`. – cmannett85 Aug 02 '12 at 17:39
  • Ah....didn't knew that, thanks, well I am stuck only on 1 issue now, I want to access a QStringList variable in a function which is defined and declared in other function. I know its a simple C++ question but I am really in urgent need of that. Thank you – Varun Chitre Aug 02 '12 at 17:45
  • You need to make it a member variable of the class that contains the method (I'm assuming it's a method, and not a 'free-standing' function), and provide a getter method to return it. You post questions rather than ask them in comments of unrelated questions, otherwise people who are suffering the same problem won't be able to search for them. And if my answer answered your original question, mark it as accepted :) – cmannett85 Aug 02 '12 at 17:58
  • "_Remember a `QStringList` is derived from `QVector< QString >`_" Last time I checked the docs, `QStringList` was a typedef of `QList` not QVector. – TrebledJ Feb 05 '19 at 14:54
1

You should reconsider using a global variable; it's usually better to pass a reference to functions that need access to it but if you must, this is how you do it. Put a definition as normal in one of your source files

QStringList foo;

and put a extern declaration in a header file that you include in all the files that you want to use it in like this

extern QStringList foo;

Wanting to pass a list of strings to a line edit also seems misguided; it would be better to just pass in a string like it expects, but if you absolutely must pass in a list you would have to subclass QLineEdit and give it a method that takes a string list and gets a string from that list which it passes to QLineEdit::setText.

Dirk Holsopple
  • 8,731
  • 1
  • 24
  • 37
  • Oh how to pass reference to other function to access its variable? it seems good idea instead of global declaration – Varun Chitre Aug 02 '12 at 16:50
  • 1
    You just declare it like any other function parameter but put a & after the type. For example, void foo(QStringList& bar). If you didn't already know this you should probably pick up a book on C++ before you proceed much farther. – Dirk Holsopple Aug 02 '12 at 16:52
  • I tried this but I get a error again while compiling with error: error: prototype for 'void MainWindow::on_pushButton_12_clicked(QString&)' does not match any in class 'MainWindow' – Varun Chitre Aug 02 '12 at 17:30
  • You're either missing the declaration of the function in the class body or the declaration doesn't match your definition. – Dirk Holsopple Aug 02 '12 at 17:37
  • No, when I remove 'QString& myvariable' from the parameter, it compiles correctly, also when I try int& myvariable' it compiles correctly but not with QString – Varun Chitre Aug 02 '12 at 17:40