Results 1 to 20 of 21

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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

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

    HelloDan (21st February 2009)

  12. #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

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

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

    HelloDan (21st February 2009)

  14. #12
    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.

  15. #13
    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

  16. #14
    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 

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
  •  
Qt is a trademark of The Qt Company.