简体   繁体   中英

Sending a file name to a qt tabWidget from main window's menu

I am using qt to build a file editor with multiple tabs, each tab corresponds to a single file. In mainwindow.cpp I have the function:

    void MainWindow::on_actionOpen_triggered()
    {

        QString fileName = QFileDialog::getOpenFileName(this,"Open File","/home",tr("*.ext"));
    }

and I need to send fileName to a new tab for my tab widget. I know there are signals and slots for classes in qt but I can't for the life of me figure out how to send the QString to a specific tab. Is there some way of sending the filename to the tab's creation event, or possibly use the tab's index for sending it a signal?

I am very new to qt but I'm fairly sure this should be a simple process. I just cannot figure it out.

Thanks in advance!

Edit: full modified code with both as members of mainwindow:

void MainWindow::on_actionOpen_triggered()
    {
    if (fileName!=NULL)
        {
        int curtab=ui->fileTabWidget->addTab(new my_editor,tr("editor"));
        ui->fileTabWidget->setCurrentIndex(curtab);
        }
    }

So following @vizhanyolajos instructions; if I pass the filename to the end of addTab, where do I need to add the rest of the code for recieving it?

I'm assuming in my custom editor class.

Your code could look something like this for example:

void MainWindow::on_actionOpen_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this,"Open File","/home",tr("*.ext"));

    createNewPage( index , name , fileName );
}


createNewPage( int index , QString name , QString fileName )
{
    // your page creation code goes here
    // your fileOpen and fill textBox code goes here where you can use the fileName for it
}

I would recommend you store your pageIndex + fileName in a container so you can easily query which page corresponds to which file.


void MainWindow::on_actionOpen_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this,"Open File","/home",tr("*.ext"));

    if (fileName!=NULL)
    {
        ui->fileTabWidget->addTab(new my_editor( fileName ),tr("editor"));
        ui->fileTabWidget->setCurrentIndex(curtab);
    }
 }

Just pass the fileName to the constructor of your my_editor Widget.
In there you can call your function for filling your editor with the fileName then.

There is no tab creation event. You need to do the following steps, and I think you don't need any SIGNAL/SLOT mechanism for this. I think that the SLOT what you included in your question is a member function of you QMainWindow. And I think your QTabWidget is also part of your QMainWindow so you only need to do this:

void onActionTriggered()
{
    const QString filename = // ...

    ui.TabWidget->addTab( /* Your custom class derived from QWidget or a Qt widget */, filename );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM