You are not logged in.

1

Thursday, May 10th 2012, 10:30pm

load file in qt

Hi ,
i am new in this website(in qt too)
i have function that read file

Source code

1
2
3
4
void my_class::read_file()
	{
		   
infile=fopen("file.obj","r+");

but i want make in the gui , button (load_file)

Source code

1
2
3
4
5
6
MainWindow::MainWindow
{
...............................................
loa_file = new QToolButton(this);
       	load_file->setObjectName(QString::fromUtf8("load_file""));
        	load-file->setGeometry(QRect(10, 10, 51, 31));

but how i make the slot to just load the file and send it to read_file
note:sorry for my english its not my native language

2

Thursday, May 10th 2012, 11:20pm

"just load the file" - What file? How will the file name be chosen if not in read_file?

If you solve that problem, then just make your read_file look like this,

void my_class::read_file(QString file)
{
...
}

and you can connect it to any signal that emits a QString - NOTE you may have to make your own signal!
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.

3

Thursday, May 10th 2012, 11:32pm

"How will the file name be chosen if not in read_file?"
i can't understand what you mean ?
"just load the file" = i want when i clic on button it show to me many files to choose one file
and i send his name to the function read_file in my_class

is that not normal?

Quoted

and you can connect it to any signal that emits a QString
the signal is not clicked to button ?

4

Friday, May 11th 2012, 8:45am

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
connect(some_button, SIGNAL(clicked()), my_class_ptr, SLOT(get_and_read_file()))

...

void my_class::read_file(QString fname)
{
  infile=fopen("file.obj","r+"); // why use fopen?  use fstream (c++) or Qfile (Qt)
}


QString my_class::get_filename()
{
  return QFileDialog::getOpenFileName();
}

// must be in section 'public slots:' or 'private slots:' in header file
void my_class::get_and_read_file()
{
  QString  filename = get_filename();
  read_file(filename);
}
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.

5

Friday, May 11th 2012, 1:07pm

Hi
i try it but it dos not work
in mainwindow.cpp
i write

Source code

1
2
3
4
MainWindow::MainWindow()
{
	my_widget = new my_class(this);
connect(repo_O, SIGNAL(clicked()), my_widget, SLOT(get_and_read_file()));

in my_class.h

Source code

1
2
3
4
5
6
7
8
9
10
11
class my_class: public my_class
{
	Q_OBJECT
..................................
public slots:
    		void get_and_read_file();
protected:
	
	virtual void paintEvent(QPaintEvent *e);
	virtual void showEvent(QShowEvent *e);
	virtual void  Read_file(QString fname);

in my_class.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void my_class::get_and_read_file()
{
  QString  filename = get_filename();
  Read_file(filename);
  }
QString my_class::get_filename()
{
  return QFileDialog::getOpenFileName();
}
void my_class:: Read_file(QString fname)
{

const char* my_file=fname.toStdString().c_str();
infile=fopen(my_file,"rb+");

but it dos not work
there is not error but it crash

6

Friday, May 11th 2012, 1:32pm

Firstly, why do you inherit paintEvent and showEvent?
Second, I would recommend you to use QFile for file operations.

Where do you assign a value to repo_O? I think the problem you get is because repo_O is uninitialized.

Jan

7

Friday, May 11th 2012, 1:40pm

"Second, I would recommend you to use QFile for file operations."
how i do that?

for the repo_O

Source code

1
2
3
4
5
 repo_O = new QToolButton(page_13);
            repo_O->setObjectName(QString::fromUtf8("repo_O"));
            repo_O->setGeometry(QRect(60, 10, 31, 31));
            repo_O->setText(QApplication::translate("MainWindow", "...", 0, QApplication::UnicodeUTF8));
			connect(repo_O, SIGNAL(clicked()), ogreWidget, SLOT(get_and_read_file()));

but that in the mainwindow not in the class my_class where there is the slot "get_and_read_file()"

8

Friday, May 11th 2012, 5:42pm

Take a look at the QFile documentation.

Source code

1
2
3
QFile file(file_name);
file.open(QFile::ReadWrite);
QByteArray contents = file.readAll();

Depending on what exactly you want to do with the file, QFile has most functions needed.

It shouldn't be a problem if the caller and the slot are in different classes.

Do you use an IDE what has a debugger? If yes, use it!

Jan

9

Friday, May 11th 2012, 5:58pm

i am not sure that i can use QFile
because created my functions nusing c++
i do not want to refer what i do
i want just loadthe file by qt

10

Friday, May 11th 2012, 6:30pm

If your program crashes then use the debugger to find out why. Learning how to debug is not optional for programmers. If you dont provide us with complete code ... (read my sig.)
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.

11

Friday, May 11th 2012, 6:46pm

I don't think you would have to include all the code you have, but if you don't you would at least have to try to debug it yourself.

Also, Qt and standard C++ actually don't go together all to well (sadly). I would recommend you to port your standard c++ functions to Qt. That would make it easier.

Jan

12

Friday, May 11th 2012, 8:21pm

I don't think you would have to include all the code you have, but if you don't you would at least have to try to debug it yourself.

Also, Qt and standard C++ actually don't go together all to well (sadly). I would recommend you to port your standard c++ functions to Qt. That would make it easier.

Jan

What? Qt is written in C++! How do they not go together?
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.

13

Saturday, May 12th 2012, 7:42pm

it show me now the QFileDialog
because i make get_file as slot
but
it show me the QFileDialog just when i run the program before i see the gui ,not when i clic on the button
in the button i make

Source code

1
connect(add_file, SIGNAL(clicked()),my_class, SLOT(get_filename()));

14

Saturday, May 12th 2012, 7:54pm

SHOW COMPLETE 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.

15

Saturday, May 12th 2012, 8:06pm

Ok

Source code

1
2
3
4
5
6
7
8
MainWindow::MainWindow()
{
    my_classe = new My_class(this);
    setCentralWidget(my_class);
add_flie= new QToolButton(this);
  add_file->setGeometry(QRect(10, 10, 31, 31));
         add_file->setText(QApplication::translate("MainWindow", "Add", 0, QApplication::UnicodeUTF8));
	 connect(add_O, SIGNAL(clicked()),my_classe, SLOT(get_filename()));

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void My_class::get_and_read_file()
{
  QString  filename = get_filename();
  read_file(filename);

}
QString My_class::get_filename()
{
	QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "ViconFormat (*.c3d)");
    QMessageBox::information(this, "Fichier", "Vous avez sélectionné :\n" + fichier);

	return fichier;
}

void My_class::read_file(QString fname)
	{
	
 my_file=fname.toStdString().c_str();

infile=fopen( my_file,"rb+");

edit: when i debug it crash here

Source code

1
infile=fopen( my_file,"rb+");

i make a small test
when i write like that

Source code

1
infile=fopen( "filename.obj,"rb+");

it work
but if i write

Source code

1
infile=fopen("C:\my_project\filename.obj","rb+");

it do not work
if i write

Source code

1
infile=fopen("C:\\my_project\\filename.obj","rb+");

it work too
there is another thing
filename we give us like that:"C:/my_project/filename.obj

This post has been edited 1 times, last edit by "chahd09" (May 12th 2012, 8:34pm)


16

Saturday, May 12th 2012, 8:30pm

Does not compile. Code is not complete. cut/paste isn't very difficult...

connect(add_O, SIGNAL(clicked()),my_classe, SLOT(get_filename()));
Why do you want clicking a button to do effectively nothing? that signal will make get_filename() happen. What does that do? returns a string, which is ignored. nett effect == 0.
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" (May 12th 2012, 8:36pm)


17

Saturday, May 12th 2012, 8:37pm

but ,
the rest of code have not relation by this problem
i work in big_project and there is many file
i can't copy all the content of file
that's my reason

18

Saturday, May 12th 2012, 9:46pm

If you are working in a big project and you do not know why

Source code

1
infile=fopen("C:\my_project\filename.obj","rb+");

wont work, but

Source code

1
infile=fopen("C:\\my_project\\filename.obj","rb+");

will, then you are in serious trouble and need to learn some c++ basics.


When I say complete code, I mean that you need to post a compilable example of your problem.

You said this

Quoted


it show me now the QFileDialog
because i make get_file as slot
but
it show me the QFileDialog just when i run the program before i see the gui ,not when i clic on the button
in the button i make

Source code

1
connect(add_file, SIGNAL(clicked()),my_class, SLOT(get_filename()));


Your question is about QFiledialog. Yet you show no code at all with QFileDialog! That is ridiculous!

You are apparently helping in a 'big project' and have fundamental lack of c++ and qt knowledge and are refusing to give relevant details.

I give up...
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.

19

Sunday, May 13th 2012, 1:30pm

Hi,
i told to yoy that the error will be in the code that i put it here
and i found it :D

Source code

1
my_file=fname.toStdString().c_str();

it must be

Source code

1
const std::string& ma_fichier=fname.toStdString();


But thanks a lot
:thumbsup:

20

Sunday, May 13th 2012, 2:10pm


What? Qt is written in C++! How do they not go together?

I didn't meant it that way. See, for example, to get an char array out of an QString (like when you want user input via a QInputDialog, for example, and then want to pass the string to a C++ function) you would either need to convert the string first to a QByteArray, and then to a char array, or you could use qPrintable, but if you don't know about that function, it would be hard to find (in the QString documentation only mentioned on one line).

There are other things to, and I've often had problems, I don't remember any specific thing right now, except the thing with QString, but I think it's generally not to easy to use normal C++ code and Qt code together.

Jan