Qt How to compare a text from textEdit - c++

I have two QTextEdit objects. In my first QTextEdit object, I have set the text. In my second QTextEdit object, I have to type in the text.
I want to compare the two texts something like this:
if(ui->textEdit2->toPlainText() == ui->textEdit1->???)
My problem is that I don't know which method to use.

if(ui->textEdit2->toPlainText() == ui->textEdit1->toPlainText())

Q: My problem is that I don't know which function I need to use....
I am not entirely sure what makes you think this would require a different method call than for your other `textEdit2. You have at least two ways to achieve this depending on your need.
The first variant would be to simply use the same method call for textEdit1 as the for textEdit2, namely:
if (ui->textEdit1->toPlainText() == ui->textEdit2->toPlainText())
Note that I swapped the order as I think it reads better and it is more comprehensive that way. It may be just my personal style, so pardon me.
If you would like to do case insensitive comparison, then you could also write the following using the static compare method of the QString class.
if (!QString::compare(ui->textEdit1->toPlainText(), ui->textEdit2->toPlainText(), Qt::CaseInsensitive))
The first solution would be too limited to do a case insensitive comparison, so pick your solution based on your exact desire.

Related

Clojure, same method call on different Java Objects

In this code headerTable and rowsTable are Java Objects. Here the same method with the same argument is being called on them:
(.setHorizontalAlignment headerTable Element/ALIGN_LEFT)
(.setHorizontalAlignment rowsTable Element/ALIGN_LEFT)
Is there a better way of doing this? I would think there must be a way to combine the two calls into one somehow. But since this is 'side effecting' code, perhaps not??
I'm thinking of an answer without writing a custom function or macro, something like "just use juxt or comp", but then maybe I'm being a bit too prescriptive...
Edit Type hinting was mentioned by Leonid Beschastny, so just in case it helps, here's the Java method signature:
public void setHorizontalAlignment(int horizontalAlignment)
And the class is PdfPTable, from iText. (This code is being used to create PDF files).
There are many possible refactorings, one would be
(run! #(.setHorizontalAlignment ^PdfPTable % Element/ALIGN_LEFT)
[headerTable rowsTable])

how to assign value in scala conditionally in template of play2.1

I am doing some conditional coding in scala template.
just tell me how to write following java logic into scala.html template.
String temp = "";
if(!cityName.equals(temp)){
temp=cityName;
}
else{
//do something..
}
Scala views allows you to define some variables with #defining block (see Reausable values), however it doesn't allow you to re-assignate it, so your pseudocode won't work as expected.
In such case you need to write custom getter in your model, which will return a valid value, instead doing it with temporary values in the views. You can also access any static Java method which will process your incoming string according to some conditions.
I must to say, that I have no idea what exactly you want to achieve, however I think, that can be solved with solutions proposed above.

Iterate and update BindingList items using a one-liner

I have a BindingList I want to update certain items,but in order to use the Foreach available only for the List<> I have to initialize a new List with the BindingList items. like this:
new List<ScanData>(ScanDataList)
.FindAll(i => i.Badge == badge)
.ForEach(x =>x.EmpName = empname);
And that's the simplest way I found to do it, but I don't want to start with the New keyword, is there any other simpler way to Iterate over the BindingList items and update them using a one-liner like the above? (I put it in three lines for readability).
Id like just to remove the New keyword but that just doesn't work,
if a new function helps that is also acceptable, if its generic for any BindingList would be perfect.
Note: Im using compact framework 2.0
I don't want to initialize a variable Im not gonna use.
Thanks.
This question is a bit silly. There's no reason you have to do it in one line of code and avoid declaring a variable. If you use the new operator you are initializing an instance of an object, regardless of whether you are declaring a variable for it or not.
That being said, I do not know what your ScanDataList is... There's a linq expression equivalent to FindAll called Where which may be more efficient than FindAll (because it doesn't have to create a new list, it just lazily iterates). If your ScanDataList is already IEnumerable then you can probably do something like this...
ScanDataList.Where(i => i.Badge == badge).ToList().ForEach(x=>x.EmpName = empname);
Even if your ScanDataList is not enumerable, you could implement an extension method of your own to help you accomplish this, but it seems like a lot of work for something that can easily be achieved without arbitrary unnecessary constraints (no new, no variables, etc).
So to clarify, I would probably use .Where LINQ expression because it is probably a bit more efficient because it doesn't need to create a new List. However, using that same logic, I'd probably avoid ToList() and separate your code into two lines with something like.
foreach(Employee emp in ScanDataList.Where(i => i.badge == badge))
emp.EmpName = empname;
This way, no additional list is created.

Table api

Suppose you have a table widget class.
Do you do table.row(i).column(0).setText(students[i].surname())
or table[0][i] = students[i].surname()
the latter makes way less sense but the simplicity is so luring ;)
ditto for: table.row(0).column(0).setBackground(red)
vs: table[0][0].setBackground(red)
Note that Table::row returns a Table::Row, whose column function returns a Table::Cell, and Table::Cell provides either setText or op= (as well as setBackground).
Same for Table::op[] and Table::Row::op[].
Your thoughts?
As a less verbose alternative for many common cases, I would also provide something like this:
table.rowcol(i, j) = "blah"; // rowcol returns directly a cell
table.colrow(k, t).SetBackground(black);
Basically the name of the method just serves as a reminder on the order of the parameters.
Also, being a single method, you can perform better exception handling IMO.
The solution with Table::row() and Table::Row::column() methods is a bit more readable (in general) and allows you to unambiguously create a Table::column() method, Table::Column (proxy) class, and Table::Column::row() method later on, if that is ever needed. This solution also makes it easy to find all places where rows/columns are accessed, which is much harder when you use operator overloading.
As others pointed out however, the second solution is less typing and, in my opinion, not much worse in readability. (May even be more readable in certain situations.)
It's up to you to decide though, I'm just giving some implications of both solutions :-)
The second one. It carries just as much meaning as the first and is hugely easier to read and to type.(I don't really understand why you say it makes "way less sense".)
Actually you'd do neither of those. They are both buggy code and could lead to exceptions if there are no rows.
if(table.rows > 0)
{
var row = table.row[0];
if(row.columns > 0)
{
var col = row.column[0];
etc...
table.row(i).column(0)
This style is known as the Named Parameter Idiom. This makes it easy to reorder a set of calls in any way that pleases you as an alternative to positional parameters. However, this works provided each of the chained calls return the same original object. Of course, in your case you have a row object and a column object. So, there isn't much to gain here.
The ease of use of the second construct provides another compelling reason to choose the latter over the former.
There is not a way which is better than another. It depends on what you have to do with your data structure.
That said, for a simple table widget (assuming you are not coding a complex Excel-like application) I'd seek easy syntax instead of a more general interface.
So table[0][1] = "blah" would just work fine for me.

Replace giant switch statement with what?

I have a code that parses some template files and when it finds a placeholder, it replaces it with a value. Something like:
<html>
<head>
<title>%title%</title>
</head>
<body bgcolor="%color%">
...etc.
In code, the parser finds those, calls this function:
string getContent(const string& name)
{
if (name == "title")
return page->getTitle();
else if (name == "color")
return getBodyColor();
...etc.
}
and then replaces the original placeholder with returned value.
In real case, it is not a dummy web page, and there are many (50+) different placeholders that can occur.
My code is C++, but I guess this problem exists with any language. It's more about algorithms and OO design I guess. Only important thing is that this must be compiled, even if I wanted I couldn't have any dynamic/eval'd code.
I though about implementing Chain of Responsibility pattern, but it doesn't seem it would improve the situation much.
UPDATE: and I'm also concerned about this comment in another thread. Should I care about it?
Use a dictionary that maps tag names to a tag handler.
You want replace conditional with polymorphism. Roughly:
string getContent(const string& name) {
myType obj = factory.getObjForName(name);
obj.doStuff();
}
where doStuff is overloaded.
Have you considered XSLT? It's very well suited to this kind of thing. I developed a content management system that did the exact same thing and found XSLT to be very effective. The parser does a lot of the work for you.
UPDATE: Steven's comment raises an important point- you'll want your templates to be valid XHTML if you decide to go the XSLT route.
Also- I would use a different delimiter for your replacement tokens. Something less likely to occur naturally. I used #!PLACEHOLDER#! in my CMS.
i'll combine 3 ideas:
(from Steven Hugig): use a factory method that gets you a different class for each selector.
(from Neil Butterworth): inside the factory, use a dictionary so you get rid of the big switch(){}.
(mine): add a setup() method to each handler class, that adds itself (or a new class instance) to the dictionary.
explaining a bit:
make an abstract class that has a static dict, and methods to register an instance with a selector string.
on each subclass the setup() method registers itself with the superclass' dict
the factory method is little more than a dictionary read
Rather than parsing, have tried just reading the template into a string and then just performing replaces.
fileContents = fileContents.Replace("%title%", page->getTitle());
fileContents = fileContents.Replace("%color%", getBodyColor());
As "Uncle" Bob Martin mentioned in a previous podacast with Joel and Jeff, pretty much anything you come up with is going to essentially be reproducing the big switch statement.
If you feel better implementing one of the solutions selected above, that's fine. It may make your code prettier, but under the covers, it's essentially equivalent.
The important thing is to ensure that there is only one instance of your big switch statement. Your switch statement or dictionary should determine which class handles this tag, and then subsequent determinations should be handled using polymorphism.