How to call a function in main.cpp from mainwindow.cpp Qt c++?
-
I have a function (called deltaOmega(&w)) in main.cpp. If one of the items qlistWidget is clicked, then this should be called this function.
in mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->listWidget->addItem("item1"); ui->listWidget->addItem("item2"); ui->listWidget->addItem("item3"); connect(ui->listWidget, &QListWidget::itemClicked, this, &MainWindow::onListItemClicked); } void MainWindow::onListItemClicked(QListWidgetItem *item) { QString clickedItemText = item->text(); if(clickedItemText == "item1") { std::cout << "item1 is clicked!" << std::endl; } }
I want to call deltaOmega(&w) function in main.cpp instead of
std::cout << "item1 is clicked!" << std::endl;
. How can i call this function from mainwindow.cpp? And how to modify mainwindow.h?Thank you.
-
@Rozerin-YILDIZ
Don't put some function you need to call frommainwindow.cpp
inmain.cpp
. Why should it be there? Nothing really ought know about that, and nothing else ought includemain.h
. Put it in its own file or some class and include the header. -
@Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:
So how do I call this function in mainwindow.cpp?
Simply call it?
-
@Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:
I have created deltaOmega.h and deltaOmega.cpp for this function, i deleted this function in main.cpp. So how do I call this function in mainwindow.cpp?
Include
deltaOmega.h
inmainWindow.cpp
, create an instance of adeltaOmega
class and call the function.// somewhere in mainWindow.cpp DeltaOmega deltaO; deltaO.deltaOmega ( ... );
The question is, what this function does and why you pass
MainWindow w
to it in yourmain.cpp
(deltaOmega(&w)
).
This would require a circular dependency which won't work now. -
When the deltaOmega() function was in main.cpp, it was like this:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; setTitleFramesBackground(&w); setLabelTitles(&w); deltaOmega(&w); w.show(); return a.exec(); }
The function for creating a chart.
-
@Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; setTitleFramesBackground(&w); setLabelTitles(&w); deltaOmega(&w); w.show(); return a.exec(); }
Yes, this can be guessed from the description, but why do you need to pass the
MainWindow
to it?
This looks like bad design. -
@Pl45m4 I needed to create a chart inside a function. I did this in deltaOmage(). I tried to show it in a widget in the mainwindow. How can I call this function by clicking on the qlistWidget I created? What kind of design do you suggest?
-
@Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:
I tried to show it in a widget in the mainwindow
What about
QWidget *deltaOmega( /* ARGS */ );
?
So the function can construct the widget and return it to
MainWindow
.// in mainwindow something like DeltaOmega *deltaO = new DeltaOmega(this); QWidget *chart = deltaO->deltaOmega(....); centralWidget()->layout()->addWidget(chart);
But I dont see why you want to do this and dont create the chart in mainWindow itself.
-
@Pl45m4 deltaOmega() function like this:
void deltaOmega(MainWindow *window) { double a[arraySize]; double b[arraySize]; loop for a; loop for b; QLineSeries *series = new QLineSeries(); for (int i = 0; i < arraySize; i++) { series->append(a[i], b[i]); } QChart *chart = new QChart(); chart->addSeries(series); chart->setTitle("Graph"); chart->setMargins(QMargins(0, -60, 0, 0)); QValueAxis *axisX = new QValueAxis; axisX->setTitleText("X"); chart->addAxis(axisX, Qt::AlignBottom); series->attachAxis(axisX); QValueAxis *axisY = new QValueAxis; axisY->setTitleText("Y"); chart->addAxis(axisY, Qt::AlignLeft); series->attachAxis(axisY); // Graph QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); QWidget *widget = window->findChild<QWidget *>("widget"); chartView->setParent(widget); chartView->resize(widget->size()); chartView->setChart(chart); }
If I don't need to do that, or if there is a better way to do it, I can follow your suggestion.
-
@Rozerin-YILDIZ The suggestion was to do all this inside MainWindow. Then you also don't have to do dirty things like:
QWidget *widget = window->findChild<QWidget *>("widget");
-
@jsulm said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:
The suggestion was to do all this inside MainWindow. Then you also don't have to do dirty things like:
@Rozerin-YILDIZ either this, or like I've written here
@Pl45m4 said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:
QWidget * deltaOmega( /* ARGS */ );
if you really want to "construct" your
QChart
outside ofMainWindow
, then make at least some kind of factory to return the created widget. You can set it to yourMainWindow
within its code and don't need to pass the pointer to yourMainWindow
around.QWidget *deltaOmega() // a.k.a. "make ChartView Widget" { double a[arraySize]; double b[arraySize]; loop for a; loop for b; QLineSeries *series = new QLineSeries(); for (int i = 0; i < arraySize; i++) { series->append(a[i], b[i]); } // create new container widget for your Chart QWidget *widget = new QWidget(); QChart *chart = new QChart(); chart->addSeries(series); chart->setTitle("Graph"); chart->setMargins(QMargins(0, -60, 0, 0)); QValueAxis *axisX = new QValueAxis; axisX->setTitleText("X"); chart->addAxis(axisX, Qt::AlignBottom); series->attachAxis(axisX); QValueAxis *axisY = new QValueAxis; axisY->setTitleText("Y"); chart->addAxis(axisY, Qt::AlignLeft); series->attachAxis(axisY); // Graph QChartView *chartView = new QChartView(chart, widget); chartView->setChart(chart); chartView->setRenderHint(QPainter::Antialiasing); QHBoxLayout *layout = new QHBoxLayout(); widget->setLayout(layout); layout->addWidget(chartView); return widget; }
then in
MainWindow
something likeDeltaOmega *deltaO = new DeltaOmega(this); centralWidget()->layout()->addWidget(deltaO->deltaOmega());
I still don't see the reason why you want to do this at all, but feel free....
-
-
P Pl45m4 referenced this topic on