-2

I have a text file containing these lines:

a1     b00     2222 
a1     b01     233 
a1     b92     34444
a2     b00      2222
a2     b00      3333
a2     b01      3333

I want to read this file and filter the text using QRegExp

and fill the result into three QComboBox combo1, for the first matching case, combo2 to the second matching case, and combo3 to the third matching case. The result looks like:

 combo1 
 a1 
 a2

when selecting a2 from the combo1 then combo2 will be

 combo2 
  b00   
  b01

and when selecting b00 from combo2, combo3 will be

 combo3 
 2222 
 3333

I haven't worked with QRegExp, can someone show me how to accomplish this?

Waleed A
  • 95
  • 10

2 Answers2

2

I suggest that you don't use a QRegExp to filter this. Because you are working with a white-space delimited file it is very simple to just read this into a vector and use that to populate the QComboBoxes. For example:

ifstream foo("foo.txt");
vector<string> combos{istream_iterator<string>(foo), istream_iterator<string>()};

for(auto i = 0; i < size(combos); ++i) {
    switch(i % 3) {
    case 0:
        combo1.addItem(combos[i]);
        break;
    case 1:
        combo2.addItem(combos[i]);
        break;
    case 2:
        combo3.addItem(stoi(combos[i]));
    }
}

Live Example

Note that you could also have used a QTextStream and read these directly into a QString or even a QStringList. I chose not to in order to keep your business logic separated from your GUI logic.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 1
    What's `size` here? (EDIT) I think not all compilers do support `std::size` yet. – Ajay Jun 09 '16 at 11:21
  • @Jonathan Mee i can read the text file and fill inot vector of string, but i want to just test how QRegExp works in this example, i wanna use match and cap in this case. – Waleed A Jun 09 '16 at 11:27
  • @Ajay You're right [`size`](http://en.cppreference.com/w/cpp/iterator/size) is C++17. If you don't have C++17, you'll need to use `combos.size()`. But if you *do* have C++17, I give a good argument for why `size(combos)` is now preferred over `combos.size()` here: http://stackoverflow.com/questions/33442629/does-c-have-a-free-function-sizeobject/33442842#33442842 – Jonathan Mee Jun 09 '16 at 11:36
  • Yup, I got the idea. Thanks for sharing something new! – Ajay Jun 09 '16 at 11:38
  • 1
    @WaleedA Given the example available to me I have given you the right way to solve this. If you use a regex: ["Now you have two problems."](http://programmers.stackexchange.com/questions/223634/what-is-meant-by-now-you-have-two-problems) I'm not saying that because I'm anti-regex. In fact if you can provide an example that would be better suited to a regex then I'll be happy to update my answer. – Jonathan Mee Jun 09 '16 at 11:43
  • @JonathanMee thanks for your answer, please check my answer below. – Waleed A Jun 09 '16 at 11:46
-1

The QregExp that i need to filter (match) each line is this

    QRegExp rx("\\s*(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s*"); 
    if (rx.exactMatch(line) //to cehck if rx matches the line from the text file
      {
          QString col1 = rx.cap(1);//the first column that goes to combo1
          QString col2 = rxHeader.cap(2);//the second column that goes to combo2
          QString col2 = rxHeader.cap(3);//the third column that goes to combo3
      }

Now it works with me.

Waleed A
  • 95
  • 10
  • If you added in the code that actually dealt with the file this would work. But at least for the example provided, and probably for any whitespace delimited file, this is an inferior solution. I'd encourage you to consider [my solution](http://stackoverflow.com/a/37724418/2642059) as the preferable alternative. – Jonathan Mee Jun 09 '16 at 11:53
  • In Qt5 you'll want to use [QRegularExpression](http://doc.qt.io/qt-5/qregularexpression.html) it has an excellent write-up on it's use: http://doc.qt.io/qt-5/qregularexpression.html#details You'll find questions about how to do a task will be met with answers using the right tool here on http://www.stackoverflow.com, however if you were to read through Qt's write up and had questions about a particular point and were to post those, I'm sure you would receive very excellent answers. Should you find yourself in such a situation feel free to link the new question and I'll try to answer as well. – Jonathan Mee Jun 09 '16 at 14:23