You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

algorIThm

Beginner

  • "algorIThm" is male
  • "algorIThm" started this thread

Posts: 5

Location: Poland

Occupation: student

  • Send private message

1

Saturday, November 5th 2011, 10:47am

Weird behaviour of my form

Hello to everyone!
I'm a beginner in Qt and this is the moment I've encountered such problem that I cannot deal with. For the first time I used Qt Designer to create .ui file. For the learning sake I haven't used it before. Signals and slots works well in this tiny one-window GUI application. I have a form that contains, among others, QLineEdit and QPushButton. The QLineEdit keeps full path to the input file. The QPushButton browseButton opens QFileDialog. The QString returned by QFileDialog should be visible in the QLineEdit's field so I call setText() on QLineEdit, however QLineEdit rejects any string set by setText() method (I mean the project compiles, but nothing is displayed). The user can also edit QLineEdit manually. The textChange() signal reports any changes and calls the slot method I wrote - onFileChange(). This slot simply checks whether the new QString is empty or not and makes Load and Next buttons enabled / disabled. I add some QMessageBoxes to see what is happening. Even if I comment any conditional statements, and call setEnabled(true) on QPushButton (as it is initially disabled) it doesn't become enabled, though QMessageBox appears and says that the execution is now in this particular slot that should make QPushButton enabled. I really don't understand the behaviour of this form. This is not my first GUI application - I've written larger, more complicated ones, without such problems. As the entire application is tiny, but consists of a few files I zipped source files along with .pro file. It's in attachment.
Please have a look at this issue. I really have no idea what's wrong. I work on Debian with kernel ver. 2.6.39.2. My Qt's version is 4.7.3. I use Qt Creator.
Thanks in advance.
algorIThm has attached the following file:
  • Parser.zip (37.75 kB - 4 times downloaded - latest: Nov 8th 2011, 5:15pm)

2

Sunday, November 6th 2011, 10:48am

have you used the debugger to step through and look athe values and the path taken by the code?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

algorIThm

Beginner

  • "algorIThm" is male
  • "algorIThm" started this thread

Posts: 5

Location: Poland

Occupation: student

  • Send private message

3

Sunday, November 6th 2011, 2:17pm

I haven't until you asked. I must admit I have no experience in debugging GUI apps. I set break point on the slot method called when Browse button is clicked, which is:

Source code

1
2
3
4
5
6
7
8
9
10
void ParserWindow::browse()
{
	QString file = "";
	file = QFileDialog::getOpenFileName(this,tr("Open file"),QString("."));
	if(!file.isEmpty())
	{
	//QMessageBox::information(this,tr("Chosen file"),file);
	lineEditInputFile->setText(file);
	}
}

Local variable QString file is firstly initialized with empty string, then after I choose file in QFileDialog it contains full path. The if statement lets in. At the moment the next instruction is

Source code

1
lineEditInputFile->setText(file);
I step inside, but instead of seeing what is happening inside setText() I can see that program destroys the local variable: file.
Story is the same when it comes to the other slot method that is called whenever QLineEdit's text changes.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void ParserWindow::onFileChange()
{
	//QMessageBox::information(this,"Message","I'm in onFileChange()");
	if(lineEditInputFile->text().isEmpty())
	{
	buttonNext->setEnabled(false);
	buttonLoad->setEnabled(false);
	}
	else
	{
	buttonNext->setEnabled(true);
	buttonLoad->setEnabled(true);
	}
}

The slot is executed. I choose to step into the if condition - the QLineEdit's text is empty. Again when I step into

Source code

1
2
buttonNext->setEnabled(true);
	buttonLoad->setEnabled(true);
execution goes to the end of the method, no operations are made on buttons, thus they remain disabled.

4

Sunday, November 6th 2011, 3:32pm

after this: lineEditInputFile->setText(file);

do
string dbg = lineEditInputFile->text();

and see what the value assigned to dbg is. if it isok, then your linedit text is getting overwritten later, and you are looking at the wrong place
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

algorIThm

Beginner

  • "algorIThm" is male
  • "algorIThm" started this thread

Posts: 5

Location: Poland

Occupation: student

  • Send private message

5

Sunday, November 6th 2011, 4:41pm

First of all thanks for helping me. The more I investigate it, the more I find it strange. I made QString dbg private member of ParserWindow. Then I modified these two problematic methods in this way:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void ParserWindow::browse()
{
QString file = "";
file = QFileDialog::getOpenFileName(this,tr("Open file"),QString("."));
if(!file.isEmpty())
{
lineEditInputFile->setText(file);
dbg = lineEditInputFile->text();
QMessageBox::information(this,tr("dbg"),lineEditInputFile->text());
}
}
void ParserWindow::onFileChange()
{
QMessageBox::information(this,"Message1",dbg);
QMessageBox::information(this,"Message2",lineEditInputFile->text());
if(lineEditInputFile->text().isEmpty())
{
buttonNext->setEnabled(false);
buttonLoad->setEnabled(false);
}
else
{
buttonNext->setEnabled(true);
buttonLoad->setEnabled(true);
}
}

And the result is:
After I type some text manually in QLineEdit Message1 and Message2 pops up without any text inside (in this scenario browse() is not called). When I click then Browse button, browse() is called, and I choose a file. dbg window pops up with full path to chosen file, but neither Message1 nor Message2 doesn't appear. QLineEdit's field also doesn't display path to the file while it should. And now interesting thing. When I type some random text in QLineEdit field Message1 and Message2 appears, but not with that random text but with the path to the file I pointed just a moment ago.
I completely don't understand this. Could you please (or someone else) download this project (it's in the attachment to my first post) and examine it? As I said it's the first time I used Qt Designer - maybe I've done some silly thing about it, or maybe this class - ParserWindow which is derived from generated by Qt UIC Ui_ParserWindow mess. Neither I nor my 2 friends knows what's the matter. We would appreciate if someone help to find the solution, as this is a part of our common project. I'd implement this form without Qt Designer as I used to do, but the assumption is that it should be easily modifiable. After all I'm not sure whether the problem really is with the .ui file.

algorIThm

Beginner

  • "algorIThm" is male
  • "algorIThm" started this thread

Posts: 5

Location: Poland

Occupation: student

  • Send private message

6

Tuesday, November 8th 2011, 9:30am

Please, can someone help me?

7

Tuesday, November 8th 2011, 5:15pm

why do you think message1 or message2 will appear after clicking browse button?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

8

Tuesday, November 8th 2011, 5:41pm

your problem is you setup the ui too many times. It should only ever be done inside the widget, not in the main!


edit:
what you are doing is making two lots of widgets (everything on the form), in exactly the same place. You are only changing the text to one of the lineedits - only one is connected to a slot. The first lot of widgets are then hidden when you setupui again in main.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Nov 8th 2011, 10:39pm)


algorIThm

Beginner

  • "algorIThm" is male
  • "algorIThm" started this thread

Posts: 5

Location: Poland

Occupation: student

  • Send private message

9

Wednesday, November 9th 2011, 11:25am

Thanks a lot for complete explanation. This of course solves my problem :-)