qt textbrowser name save in an array - c++

I am trying to show my 4by4 matrix in qt gui, there for I have used one text browser for each element of matrix. Right now I am able to display matrix using switch case but I dont like this method. I want to make an array in which I can save the name of textbrowser and willing to access them using for loop. below is the my current code. please guide me how can I get what I am willing to do.
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 3;j++)
{
switch(no){
case 1:
ui->textBrowser_200->setText(text1);
break;
case 2:
ui->textBrowser_201->setText(text1);
break;
case 3:
ui->textBrowser_202->setText(text1);
break;
case 4:
ui->textBrowser_203->setText(text1);
break;
case 5:
ui->textBrowser_204->setText(text1);
break;
case 6:
ui->textBrowser_205->setText(text1);
break;
case 7:
ui->textBrowser_206->setText(text1);
break;
case 8:
ui->textBrowser_207->setText(text1);
break;
case 9:
ui->textBrowser_208->setText(text1);
break;
case 10:
ui->textBrowser_209->setText(text1);
break;
case 11:
ui->textBrowser_210->setText(text1);
break;
case 12:
ui->textBrowser_211->setText(text1);
break;
case 13:
ui->textBrowser_212->setText(text1);
break;
case 14:
ui->textBrowser_213->setText(text1);
break;
case 15:
ui->textBrowser_214->setText(text1);
break;
case 16:
ui->textBrowser_215->setText(text1);
break;
}
no++;
}
}

Here is a simple solution which will allow you to keep your current Designer ui:
Add this member variable to your own class, to have two-dimensional matrix of widget pointers:
std::array< std::array <QTextBrowser *, 4>, 4> mTextBrowserMatrix;
Then initialize it in the constructor, with 16 lines of code like this, after you have called setupUi() for the Designer ui:
mTextBrowserMatrix[0][0] = ui->textBrowser_200;
// repeat above for all 16 widgets.
And then just access them like mTextBrowserMatrix[0][0] etc.
You could use any container or even plain C arrays for this, above is just an example.
Of course you will save some repetitive copy-paste style code and have cleaner Designer design, if you just create the QTextBrowser matrix in code, instead of using Designer for it. But since you already have them, might as well stick with it for now, 16 lines of repeated code is not that horrible.

Related

how does this switch block executes?

#include<bits/stdc++.h>
using namespace std;
void show(int errorCause)
{
switch(errorCause)
{
case 1:
{
cout<<"in 1\n";
break;
}
case 2: break;
case 3:
{
cout<<"in 3\n";
break;
case 4:
{
cout<<"in 4\n";
case 5: cout<<"in 5\n";
break;
}
}
break;
default:
{
cout<<"in deafult\n";
break;
}
}
return;
}
int main()
{
show(5);
return 0;
}
I used this sample of code and I could not figure out its flow.According to me it should match the default condition as the errorCause does not match anything,but its output is:
in 5
I don't understand why it is not going to default condition?
Here is my build environment details:
compiler:
g++ version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
System:
Ubuntu 14.04(64-bit)
You pass 5, why should the switch statement not go into 'case 5'?
To make it clear: Remove all these curly braces inside the switch-block, none of them is necessary. The re-align and format the code, then it should be clear.
case/default labels for a switch statement may appear anywhere within that switch statement, except within a nested switch statement.
A famous example of this usage is Duff's device for unrolling loops:
void copy(unsigned char *to, const unsigned char *from, size_t count)
{
size_t n;
if (!count)
return;
n = (count + 7) / 8;
switch (count % 8) {
case 0:
do {
*to++ = *from++;
case 1:
*to++ = *from++;
case 2:
*to++ = *from++;
case 3:
*to++ = *from++;
case 4:
*to++ = *from++;
case 5:
*to++ = *from++;
case 6:
*to++ = *from++;
case 7:
*to++ = *from++;
case 1:
} while (--n > 0);
}
}
(adapted from the original).
At first glance, that doesn't make any sense (and it is somewhat redundant if you allow the compiler to unroll loops for you), but it illustrates that case labels can be placed more or less where you like within the switch statement.
First, don't write code like that. <g>
Second, the reason that it gets to case 5: is simply that there's a case 5: inside the switch statement. It doesn't matter that it's nested inside two levels of curly braces; it's just a label for the code to jump to. It doesn't have to be at the outer level of the switch statement.
It's because actually the switch statement evaluation is "relaxed", so the braces do not matter there. Only case matters, but you can jump right into the middle of a scope by the case (or even to the middle of a loop, see Duff's device).
because the value you passed is 5 , which exactly matches with the switch case parameter.
case 5: cout<<"in 5\n";
break;
if you want to get the default statement then modify the main function as shown below :
int main()
{
show(6);
return 0;
}
hope this helps.

C++ , Switch case in a switch case

Is it possible to have a switch case in a switch case 3 times? So if I have the main switch case and three in it, like:
switch(bla)
case 1:
switch(bla2)
bla
break;
case 2:
switch(bla2)
bla
break
case 3:
switch(bla2)
bla
break
So bla 2is the same string that I´m checking on.
Yes, you may nest switch statements.
Unlike in your example, you would have to make proper use of braces ({}) so that it is clear to the computer what you want to do. I suppose you'd have to actually decide what you want to do, first.
Yes, Nested switches are possible. However you must use "{}".
switch(ch1) {
case 'A':
cout << "This A is part of outer switch";
switch(ch2) {
case 'A':
cout << "This A is part of inner switch";
break;
case 'B': // ...
}
break;
case 'B': // ... }
Check this out : http://www.tutorialspoint.com/cplusplus/cpp_nested_switch.htm

c++ classes switch case

switch(choice)
{
case 1:
uinstance1.addNewProduct(data);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
uinstance1.listAllProducts(data);
break;
case 8:
break;
case 9:
break;
case 10:
//name,category,barcode,price,manufacturer,noinstock,soldpermonth,expirydate,discount
// Perishable(string,string,string,double,string,int,int);
Perishable item0("Ferrari","Automobile","9999",2999.99,"Popular",5,0);
data.addNew(item0);
break;
default:
cout<<"Wrong Choice "<<endl;
system("pause");
break;
}
}
Hi ,i have been thinking about this error for quite some time and cant seem to figure out the issue.
error C2361: initialization of 'item0' is skipped by 'default' label
: see declaration of 'item0'
Some help would be appreciated.
Thanks
The whole select block counts as one scope, if you decalare a variable in that scope you need to initialize it in every case statement (every possible execution path). You can avoid it by creating a additional scope in your case to avoid the problem (see the brackets):
switch(choice)
{
case 1:
uinstance1.addNewProduct(data);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
uinstance1.listAllProducts(data);
break;
case 8:
break;
case 9:
break;
case 10:
{
//name,category,barcode,price,manufacturer,noinstock,soldpermonth,expirydate,discount
// Perishable(string,string,string,double,string,int,int);
Perishable item0("Ferrari","Automobile","9999",2999.99,"Popular",5,0);
data.addNew(item0);
}
break;
default:
cout<<"Wrong Choice "<<endl;
system("pause");
break;
}
}
MSDN explains the error C2361 aptly:
The initialization of identifier can be skipped in a switch statement. You cannot jump past a declaration with an initializer unless the declaration is enclosed in a block. (Unless it is declared within a block, the variable is within scope until the end of the switch statement.)
Always pay attention to the error numbers they provide vital information about why the error.
You forgot the braces in one of the cases.
case 10:
{
^^^
Perishable item0;
data.addNew(item0);
break;
}
^^^
Your label is crossing initialization which is illegal. Pretty sure moving default to the top should fix it. You can also add code blocks { } around your relevant code. If you still have problems then move your object outside of the switch block.
You can't create variables inside case statements if you don't define explicity the scope.
There is another discussion about that: Variables inside case statement
case 10:
{ // <<-- This gives explicit scope for the stack variable and let's you get rid of the error
Perishable item0;
// ...
}
break;

Dynamically changing size of switch-case

Following situation:
My system gets an hardware signal and writes a time value to a buffer in my
signal handler routine. Afterwards a (software) signal is sent with the time value as argument to the appropriate slot function.
The slot routine gets called correctly, but here my problem lays in:
In the slot function I have a simple switch-case statement like this:
switch(id) {
case 1:
do something..
id = 2;
break;
case 2:
start_time = val;
id = 3;
break;
case 3:
end_time = val;
id = 1;
break;
}
In those three cases I store a start and end time value between case 2 and 3 and
out of those time values I determine the elapsed time between the hardware
signals. This works fine, but now I have to measure the time sometimes "longer",
depening on parameter. This means, I can't stop the measurement at case 3 instead
I have case 4, 5, 6 and so on . What is an elegant and optimal solution for this "problem"
instead of writing:
if (param < xy) {
switch(id) {
case 1:
...
break;
case 2:
...
break;
} else if (param > xy) {
switch(id) {
case 1:
...;
break;
case 2:
...;
break;
case 3:
...;
break;
case 4:
...;
break;
case 5:
...;
break;
}
}
}
What you are describing is called a finite state machine there are a large number of excellent state machine libraries out there that will take care of the heavy lifting for you.
Take a look at this question and some of the others that it references.
You can try following:
switch ((param - xy) >= 0 ? id : -id) {
// param >= xy cases
case 1:
...
break;
case 2:
...
break;
...
// param < xy cases
case -1:
...
break;
case -2:
...
break;
...
}
Or for something fun an exciting, you could write some self modifying code to dynamically change your swithc jump table as the parameters it receives differ. You'd have to allocate a large enough area for the largest table size and play around with funciton pointers or assembler, but it could be done.
Try using a std::map of function pointers, a.k.a. jump table, rather than a switch statement. The map allows flexibility during run-time.
Store a pointer to the function, along with the case value. Search the map for the case value, retrieve the pointer and dereference to call the function.

How to handle "switch/case" when new parameters added in SW release

How to handle when there is a new SW Release sometime and it adds another index to switch case. Index represents a parameter in this case. For example,
Rel1: i = 1-5, 7 (excluding 6)
Rel2: i = 1-7
for (int i = 1; i<=7;i++)
{
switch (i)
{
case 1: /*process data*/ break;
case 2: /*process data*/ break;
case 3: /*process data*/ break;
case 4: /*process data*/ break;
case 5: /*process data*/ break;
// case 6: // REL 2
case 7: /*process data*/ break;
default: break;
}
}
Can I actually check by adding if statement between those cases? Any better idea?
#define which release is this and then use #ifdef.
switch (i)
{
case 1: /*process data*/ break;
case 2: /*process data*/ break;
case 3: /*process data*/ break;
case 4: /*process data*/ break;
case 5: /*process data*/ break;
#ifdef REL_2
case 6: /*process data*/ break; // <-- executed only for REL_2
#endif
case 7: /*process data*/ break;
default: break;
}
In production code if I have an unexpected result like this I often try to capture and log it.
Throwing an exception may be ok depending on how it's handled. For future-proofing it's usually good to design stuff like this to gracefully handle new values.
Instead of using a 'for' loop, use a 'while' loop with a release number generator function.
Pseudo code:
typedef enum {rel1, rel2} rel_t;
// Where 'ReleaseSequence' is a generator functor. Class that takes rel_t
// in constructor and creates an appropriate functor that returns the
// required sequence of numbers for a particular release
ReleaseSequence seq(rel1);
while (i = seq()) {
switch (i) {
case 1:...
case 2:...
case 3:...
case 4:...
case 5:...
case 6:...
case 7:...
default:
break;
}
}
I find it very strange to see a switch nested within a loop...
It looks like you are executing a pipe. Then why don't you simply define is as one.
For example, a simple array of pointer to functions would do nicely. You can define one such pipe per release, and provide "noop" functions for the parameters to ignore.
If you want to catch unexpected values, shouldn't you put that check in the default part?
...
default:
assert(false); // We should never get here!