Important: Please read the Qt Code of Conduct -

How to manipulate data on one.cpp using two.cpp?

  • Here's the thing: I'm both new to C++ and to Qt. I was able to create conn.h/conn.cpp to make a connection to an SQLite3 db used on mainwindow.cpp. My point is that I was able to use code from conn .cpp file on my mainwindow.cpp file. But...

    I have several lineEdits on my main page that I want to be able to access from another .cpp, for writing them to the db. I've tried:


    but none of my edits come up. I get some attachments for MainWindow but not any of the edit boxes. I could do all of this from mainwindow.cpp, but that'll only serve to crowd up the page. So I decided to create another .h and .cpp to handle writing to the db.

    How can I harvest the data in my lineEdits on mainwindow.cpp using writeData.cpp? I'm not here to roach code. If there's information on this that I can access, send me a link. I appreciate the help. Thanks.

  • Lifetime Qt Champion


    Please take a look at the numerous examples and tutorials that are available in Qt's documentation.

    As for your problem, you're starting on the wrong foot. From your description, you are trying to make your widgets available everywhere while in realty it's rather their content that should be passed on. So you should make a proper interface to write your data wherever you want and then pass the content of your line edits to it.

    That will:

    • keep a clean separation between your logic and your GUI
    • keep responsibilities clear
    • avoid tight coupling

  • Isn't that what I'm doing? I'm confused now. I thought that by accessing the lineEdits, I'd be able to harvest their content. That's not the way of it?

  • Lifetime Qt Champion

    Based on what you wrote, you are trying to pass your QLineEdits around, which is not the best way to do it. However without any code, it's next to impossible to give you an accurate answer.

  • Hello SGaist:

    You're correct. That's what I'm attempting to do. What's the best way? I'm looking at the SQL examples right now, but I don't see what you might be talking about...not yet. I really don't know what I'm looking for, truth told.

    This is just a small program for my girlfriend, to hold her recipes. I would say it's of no big consequence, due to that. But if I messed up her recipes, she'd skin me up :-)

  • Lifetime Qt Champion

    What exactly are you trying to achieve ?

  • What I want to do is write data from the lineEdits on mainwindow.cpp to an SQLite3 database. I can do this by placing all the code on mainwindow.cpp. But what I want to do is to be able to harvest the lineEdits' content on mainwindow.cpp using another .cpp file. I want another .cpp to do the work of harvesting and writing to the database. mainwindow.cpp will hold the content in the lineEdits. A second .cpp will harvest that content and write it to the database. That's what I want. I thought I would be able to do that, but I think I might have misjudged what I am, and am not, able to do within the confines of Qt. My knowledge is limited. I'm sure this is possible; I just don't know how to accomplish it.

  • Lifetime Qt Champion

    @landslyde In this "another cpp" you should not access mainwindow stuff. Instead mainwindow should pass the data to it. Like:

    // another cpp
    class Harvester
        void setData(const QString&);
    // in mainwindow.cpp

    class MainWindow: public QMainWindow
    Harvester harvester;

    // in mainwindow.cpp where you want to pass data to harvester

    This way harvester does not need anything about mainwindow and its internal implementation details.

  • Hi jsulm,

    Thank you for your response. I understand exactly what you're talking about. This is what I needed, I just didn't know how to go about it. I'll get busy with this and see if I can accomplish what you laid out for me. Thanks again. Very much appreciated.

Log in to reply