Results 1 to 6 of 6

Thread: Need help asap! Filestream

  1. #1
    Join Date
    May 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Exclamation Need help asap! Filestream

    Hi! I'm so new to qt and i don't know how to do a filestream. I am creating a login form-filestream based. So i need to create a login form which has of course username and password. First what i did was to use SQLITE as a database and it worked. But the problem is. I have to use filestream that would end up making a new .txt file where the username and passwords would be inputed. I don't know the proper codes to be used in making a filestream from qt and i don't know where in my main code i should put it. i badly need help. The codes that are written there are for sqlite base but i need to change it to a filestream type that would be based from a .txt file. Here's my code:

    Qt Code:
    1. #include "login.h"
    2. #include "ui_login.h"
    3. #include <QMessageBox>
    4. #include <QPixmap>
    5. #include <QFont>
    6. #include <QSplashScreen>
    7. #include <QTimer>
    8.  
    9. Login::Login(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::Login)
    12. {
    13. ui->setupUi(this);
    14. QPixmap pix("C:/Users/Patch/Documents/COE113PROJ/Login/LOGINFORM/images/background.jpg");
    15. ui->background->setPixmap(pix);
    16.  
    17. mydb = QSqlDatabase::addDatabase("QSQLITE");
    18. mydb.setDatabaseName("C:/Users/Patch/Documents/COE113PROJ/Login/LOGINFORM/sqlite/users.db");
    19.  
    20. if(!mydb.open())
    21. ui->status->setText("Failed to open the database");
    22.  
    23. else
    24. ui->status->setText("Connected Succesfuly");
    25. }
    26.  
    27. Login::~Login()
    28. {
    29. delete ui;
    30. }
    31.  
    32. void Login::on_pushButton_login_clicked()
    33. {
    34.  
    35.  
    36. QString name,password;
    37. name=ui->lineEdit_Username->text();
    38. password=ui->lineEdit_Password->text();
    39.  
    40. if(!mydb.isOpen()){
    41.  
    42. qDebug()<<"Failed to open the database";
    43. return;
    44. }
    45.  
    46. QSqlQuery qry;
    47.  
    48. if(qry.exec("select * from users.dbhalp.jpg where name='"+name+"'and password='"+password+"'"))
    49. {
    50. int count=0;
    51. while(qry.next())
    52. {
    53. count++;
    54. }
    55. if(count==1)
    56. {
    57. QMessageBox::information(this,"Login","Username and password is correct ");
    58. hide();
    59. secDiag = new SecDiag(this);
    60. secDiag->show();
    61.  
    62. }
    63.  
    64. if(count>1)
    65. {
    66. QMessageBox::information(this,"Login","Username and password is incorrect");
    67. }
    68. if(count<1)
    69. {
    70. QMessageBox::information(this,"Login","Username and password is incorrect");
    71. }
    72.  
    73.  
    74. }
    75.  
    76.  
    77.  
    78.  
    79. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 5th May 2016 at 16:35. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help asap! Filestream

    if you want to write into a text file, just use QFile to open the file, pass it to a QTextStream instance and write using that stream.

    Cheers,
    _

  3. #3
    Join Date
    May 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Need help asap! Filestream

    I got the command for the QFile. my text file for example looks like this: "Patch"|"Cool" . i got it by exporting the database from SQLITE Manager. Where Patch should represent the username then Cool would act as the password. My problem is that how can I declare that the term "Patch" is the username as well as for the password.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Need help asap! Filestream

    Use the QTextStream::operator>>() to read the file into a QString. Use QString::split() with '|' as the split character to split the string into a QStringList. This should have a size of 2. Use QStringList::at() with the index 0 to extract the user name into your username QString variable, use the same method with index 1 to get the password into its QString variable. If you need to remove the quotes, you can use QString::remove().

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help asap! Filestream

    Quote Originally Posted by Patch View Post
    My problem is that how can I declare that the term "Patch" is the username as well as for the password.
    This is a text file, you don't need to declare anything.
    You interpret the data according to whatever your format says when reading.

    Quote Originally Posted by d_stranz View Post
    Use the QTextStream::operator>>() to read the file into a QString.
    I would suggest using QTextStream::readLine() instead.
    The stream operator only reads until the first white space, content can often contain these (here the password could, for example).

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    d_stranz (6th May 2016)

  7. #6
    Join Date
    May 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Need help asap! Filestream

    I got this one

    void Login:n_pushButton_login_clicked()
    {

    char str[619];//and his name is reyy misterio
    QString name,password;
    string infile_name,infile_password;
    string infile_fullname;
    name=ui->lineEdit_Username->text();
    password=ui->lineEdit_Password->text();

    //the login source codes
    ifstream infile("C:/Users/Patch/Documents/COE113PROJ/Login/LOGINFORM/sqlite/users4.txt");
    infile.getline(str,619,';');
    infile_name = str;
    infile.getline(str,619,';');
    infile_password = str;
    infile.getline(str,619,'\n');
    infile_fullname = str;



    while(!infile.eof()){



    if(infile_name == name.toStdString() && infile_password == password.toStdString())
    {

    QMessageBox::information(this,"Login","Username and password is correct ");
    hide();
    secDiag = new SecDiag(this);
    secDiag->show();

    break; //This will display the next dialog
    }

    else //this will be used as a false statement


    QMessageBox::information(this,"Login","Username and password is incorrect");



    infile.getline(str,619,';');
    infile_name = str;
    infile.getline(str,619,';');
    infile_password = str;
    infile.getline(str,619,'\n');
    infile_fullname = str;



    it is actually working but there is one problem. The value for example the "infile_name" never changes. it stays as "name" and if it is compared to the Qstring variable "name" it is always false. Know a solution?

Similar Threads

  1. Replies: 1
    Last Post: 20th January 2014, 04:02
  2. problems with usbExample ASAP
    By david_itay in forum Newbie
    Replies: 2
    Last Post: 7th August 2012, 19:52
  3. Asap!!!! Qt duck shoot
    By audrensitas in forum General Discussion
    Replies: 2
    Last Post: 28th April 2011, 12:33
  4. Please help ASAP::QColorDialog
    By LiCodeX in forum Newbie
    Replies: 2
    Last Post: 2nd October 2007, 13:16

Tags for this Thread

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.