Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: How to use a file to setup a QProgressDialog, Thanks

  1. #1
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default How to use a file to setup a QProgressDialog, Thanks

    Hi, all.
    I want to read a plaintext file in my program, and show a QProgressDialog during reading. But i don't know how to detect the size of my file to set it up.

    It must be a very simple question. But i have not idea about it. Could anyone help?
    Thanks for any help!

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    use size() of QFile..next, depending upon the number of bytes read compared to total size, call setValue()

  3. #3
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    use size() of QFile..next, depending upon the number of bytes read compared to total size, call setValue()
    Thanks! I will try.

  4. #4
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    use size() of QFile..next, depending upon the number of bytes read compared to total size, call setValue()
    That's a problem, is that any way to try my thinking in a console window? I want to try QProgressDialog in a console window.

    Thanks

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    why would u want a QProgressDialog in a console window?

  6. #6
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Try to test if my code works.

    Do you have any good ways to test?

  7. #7
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    why would u want a QProgressDialog in a console window?
    Is it OK? I wrote it in this way, just for try

    Qt Code:
    1. QFile file("in.txt");
    2. QTextStream in(&file);
    3.  
    4. QProgressDialog progressDialog();
    5. progressDialog.setCancelButtonText(tr("&Cancel"));
    6. progressDialog.setRange(0, file.size());
    7. progressDialog.setWindowTitle(tr("Loading..."));
    8. QString line;
    9. while(file.pos()<file.size())
    10. {
    11. progressDialog.setValue(file.pos());
    12. progressDialog.setLabelText(tr("Searching file number %1 of %2...")
    13. .arg(file.pos()).arg(file.size()));
    14. qApp->processEvents();
    15.  
    16. if (progressDialog.wasCanceled())
    17. break;
    18. line=in.readLine();
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    looks good..isnt it working?

  9. #9
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    looks good..isnt it working?
    Thanks. I have try the code in my app. That segment of my code may be OK.
    But that is still some problems.

    1 I use QFileDialog::getOpenFileName to get a file. But i doesn't reveal immediately in the QComboBox, but it does in the comboBox which I can use my mouse to select it from the drop down list. I post my code in the following. Why?

    Qt Code:
    1. void DataConvertor::inputFileNameBrowse()
    2. {
    3. QString initialName=inputFileNameComboBox->currentText();
    4. if(initialName.isEmpty())
    5. initialName=QDir::homePath();
    6. QString inputFileName=QFileDialog::getOpenFileName(this,
    7. tr("Open file"), initialName);
    8. inputFileName=QDir::toNativeSeparators(inputFileName);
    9.  
    10.  
    11. if(!inputFileName.isEmpty())
    12. {
    13. inputFileNameComboBox->addItem(inputFileName);
    14. //when it's empty, the index is 0.
    15. inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->currentIndex() + 1);
    16. //inputFileNameComboBox->activated(inputFileNameComboBox->currentIndex());
    17.  
    18. // set output file the same location with the inputFile if it is empty.
    19. if(outputFileDirectoryComboBox->currentText().isEmpty())
    20. {
    21. outputFileDirectoryComboBox->addItem(QDir::toNativeSeparators(QFileInfo(inputFileName).path()));
    22. outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->currentIndex() + 1);
    23. //outputFileDirectoryComboBox->activated(outputFileDirectoryComboBox->currentIndex());
    24. }
    25.  
    26. okButton->setEnabled(true); // enable the ok button
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    looks good..isnt it working?
    2 This's problem is about the QProgressDialog. Only When I click the OK button for several times could my program works, it will turn up several QprogressDialogs. Also, That is not output file, I cann't find it.

    Thanks!

    Qt Code:
    1. DataConvertor::DataConvertor(QWidget *parent):QDialog(parent)
    2. {
    3. setupUi(this);
    4. okButton->setEnabled(false); // disable OK button
    5.  
    6. connect(inputFileBrowseButton,SIGNAL(clicked()),this,SLOT(inputFileNameBrowse()));
    7. connect(outputFileBrowseButton,SIGNAL(clicked()),this,SLOT(outputFileDirectoryBrowse()));
    8. connect(aboutButton,SIGNAL(clicked()),this,SLOT(aboutinfo()));
    9. connect(okButton,SIGNAL(clicked()),this,SLOT(processFile()));
    10.  
    11. }
    12.  
    13.  
    14. void DataConvertor::processFile()
    15. {
    16. QString fileName=inputFileNameComboBox->currentText();
    17. if(fileName.isEmpty())
    18. {
    19. QMessageBox::information(this,tr("Data Convertor"),tr("Not such a file, Please reselect a file."),QMessageBox::Ok);
    20. }
    21. else
    22. {
    23. QFile file(fileName);
    24. //Open read file.
    25. if(!file.open(QIODevice::ReadOnly))
    26. {
    27. QMessageBox::information(this,tr("Data Convertor"),tr("Cann't read file %1:\n%2.").arg(file.fileName()).arg(file.errorString()),QMessageBox::Cancel);
    28. return;
    29. }
    30. //Open write directory.
    31. QString outDirName=outputFileDirectoryComboBox->currentText();
    32. QDir outDir(outDirName);
    33. ///
    34. //if the directory is not existed, create one.
    35. if(!outDir.exists())
    36. {
    37. outDir.mkpath(outDirName);
    38. }
    39.  
    40. QTextStream in(&file);
    41.  
    42. // form outfile name.
    43. QString path=outDirName+QDir::separator()+"abc.txt";
    44. QFile outFile(path);
    45. QTextStream out(&outFile);
    46.  
    47. // setup QProgressDailog
    48. QProgressDialog progressDialog(this);
    49. progressDialog.setCancelButtonText(tr("&Cancel"));
    50. progressDialog.setRange(0, file.size());
    51. progressDialog.setWindowTitle(tr("Loading..."));
    52. QString line;
    53.  
    54. while(file.pos()<file.size())
    55. {
    56. progressDialog.setValue(file.pos());
    57. progressDialog.setLabelText(tr("Searching file number %1 of %2...")
    58. .arg(file.pos()).arg(file.size()));
    59. qApp->processEvents();
    60.  
    61. if (progressDialog.wasCanceled())
    62. break;
    63. line=in.readLine();
    64. out<<line;
    65. }
    66. }
    67. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    although i dont understand clearly what ur problem is, but use:

    Qt Code:
    1. void QComboBox::setEditText ( const QString & text ) [slot]
    2.  
    3. Sets the text in the combobox's text edit.
    To copy to clipboard, switch view to plain text mode 

    instead of setCurrentIndex() in both combo boxes..ur problem should solve

  12. The following user says thank you to talk2amulya for this useful post:

    HelloDan (21st February 2009)

  13. #12
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    dont disable OK button in DataConverter, disable it at the start of processFile() and enable it again at the end of it.

  14. The following user says thank you to talk2amulya for this useful post:

    HelloDan (21st February 2009)

  15. #13
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    dont disable OK button in DataConverter, disable it at the start of processFile() and enable it again at the end of it.
    Thanks very much! I had tried. It works.

    But the QComboBox drop down list problem is still that.

    By the way, how can i start a new line in the file, what about in this way: out<<line<<"\n";
    I find the following code doesn't work properly, the data is out of order and miss some
    Qt Code:
    1. while(file.pos()<file.size())
    2. {
    3. progressDialog.setValue(file.pos());
    4. progressDialog.setLabelText(tr("Processing file number %1 of %2...")
    5. .arg(file.pos()).arg(file.size()));
    6. qApp->processEvents();
    7.  
    8. if (progressDialog.wasCanceled())
    9. break;
    10. line=in.readLine();
    11. out<<line<<"\n";
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by HelloDan; 21st February 2009 at 16:43.

  16. #14
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    could u tell EXACTLY what the problem is..i m still not clear about it

  17. #15
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    could u tell EXACTLY what the problem is..i m still not clear about it
    Qt Code:
    1. void DataConvertor::inputFileNameBrowse()
    2. {
    3. QString initialName=inputFileNameComboBox->currentText();
    4. if(initialName.isEmpty())
    5. initialName=QDir::homePath();
    6. QString inputFileName=QFileDialog::getOpenFileName(this,
    7. tr("Open file"), initialName);
    8. inputFileName=QDir::toNativeSeparators(inputFileName);
    9.  
    10.  
    11. if(!inputFileName.isEmpty())
    12. {
    13. inputFileNameComboBox->addItem(inputFileName);
    14. //when it's empty, the index is 0.
    15. inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->currentIndex() + 1);
    16. //inputFileNameComboBox->activated(inputFileNameComboBox->currentIndex());
    17.  
    18. // set output file the same location with the inputFile if it is empty.
    19. if(outputFileDirectoryComboBox->currentText().isEmpty())
    20. {
    21. outputFileDirectoryComboBox->addItem(QDir::toNativeSeparators(QFileInfo(inputFileName).path()));
    22. outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->currentIndex() + 1);
    23. //outputFileDirectoryComboBox->activated(outputFileDirectoryComboBox->currentIndex());
    24. }
    25.  
    26. okButton->setEnabled(true); // enable the ok button
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    In this function, after i use QFileDialog::getOpenFileName to select a file, the combox should contain the file path. But the QComboBox is empty. But I can find the file which i select using the mouse in the drop down list. At the same time, I set the same path to outputFileDirectoryComboBox, it was also hidden in the drop down list. Why?


    By the way, how can i start a new line in the file, what about in this way: out<<line<<"\n";
    I find the following code doesn't work properly, the data is out of order and miss some


    Thanks

  18. #16
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    ok, regarding combobox problem, use

    Qt Code:
    1. inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->count() - 1);
    2. and
    3. outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->count() - 1);
    To copy to clipboard, switch view to plain text mode 

    for other problem,try

    Qt Code:
    1. out<<line<<'\r\n'
    To copy to clipboard, switch view to plain text mode 

  19. #17
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    ok, regarding combobox problem, use

    Qt Code:
    1. inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->count() - 1);
    2. and
    3. outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->count() - 1);
    To copy to clipboard, switch view to plain text mode 

    for other problem,try

    Qt Code:
    1. out<<line<<'\r\n'
    To copy to clipboard, switch view to plain text mode 
    Thanks! I had tried.

    The drop down list one works well.

    The output one in this way : out<<line<<"\r\n"; is more better.
    In this way,the order is OK, but still miss the last part of the data.

  20. #18
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    could you show the written file..i wanna see where it is missing..or u can use qDebug to print all lines that u r reading and showing...then show me the output

  21. #19
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Quote Originally Posted by talk2amulya View Post
    could you show the written file..i wanna see where it is missing..or u can use qDebug to print all lines that u r reading and showing...then show me the output
    Thanks!
    for example, the inputfile format is :
    20040101 C 0 C 0 C 0 ENE 10 -1 11 0 0 -1 11 0 0
    20040102 WSW 10 NW 30 NNW 30 NNW 10 -1 7 0 0 -1 6 0 0
    20040103 NNW 20 C 0 SSE 20 SE 30 -1 8 0 0 -1 0 0 0
    20040104 C 0 NE 10 S 20 SSW 10 -1 7 6 0 -1 0 0 0
    20040105 E 10 C 0 S 20 C 0 -1 9 0 0 -1 0 0 0
    20040106 E 10 NNW 20 SW 10 NNW 10 -1 10 6 0 -1 10 0 0


    but the output one is only one line when the input data size is small.:
    20040101 C 0 C 0 C 0 ENE 10 -1 11 0 0 -1 11 0 0


    How to use a qDebug() in a GUI programming? I can make it in a console window, but fail in a GUI one. Maybe it's to fast, how I can make it pause? like system("pause");

    Thanks!

  22. #20
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use a file to setup a QProgressDialog, Thanks

    Qt Code:
    1. while(file.pos()<file.size()) //-->
    2. while(in.pos()<file.size())
    To copy to clipboard, switch view to plain text mode 

    Somebody had helped me to solve the problem.


    By the way, it's that any way to reuse output file name and QTextStream name? Such as:
    Qt Code:
    1. if(month!=list.at(0))
    2. {
    3. QFile monthFile(outDirName+QDir::separator()+list.at(0)+".txt");
    4. QTextStream monthOut(&monthFile);
    5. month=list.at(0);
    6. }
    7.  
    8. monthOut<<list.at(0)<<'\t'<<list.at(1)<<'\t'<<list.at(2)<<"\n";
    To copy to clipboard, switch view to plain text mode 

    In my previous standard C++ code, I do it in this way:
    cpp Code:
    1. while(getline(ifile, str))
    2. {
    3. filename = str.substr(0, 6)+".txt";
    4. ofstream ofile(filename.c_str(), ios_base::app);
    5. str.erase(0,10);
    6. ofile << str << endl;
    7. ofile.close();
    8. }
    To copy to clipboard, switch view to plain text mode 

    But I do think that reopen and reclose a file in this way is not a wise choice. But i can not work out a better method.

    There are lots of problem to learn in practical experiment. Thanks sincerely to all the friends help me.
    Last edited by wysota; 22nd February 2009 at 07:54. Reason: missing [code] tags

Similar Threads

  1. Apparent error in QtCore/quuid.h
    By cwp500 in forum Qt Programming
    Replies: 11
    Last Post: 18th December 2008, 20:51
  2. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 15:54
  3. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  4. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.