application in Qt 5.2.1. here is interface :-
Here is goto cell dialog header file :-
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QDialog>
#include <QtWidgets>
namespace Ui {
class gotocellDialog;
}
class gotocellDialog : public QDialog
{
Q_OBJECT
public:
explicit gotocellDialog(QWidget *parent = 0);
~gotocellDialog();
private slots:
void on_lineEditCellLocation_textChanged(const QString &arg1);
private:
Ui::gotocellDialog *ui;
};
#endif // GOTOCELLDIALOG_H
and here is gotocell.cpp
#include "gotocelldialog.h"
#include "ui_gotocelldialog.h"
gotocellDialog::gotocellDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::gotocellDialog)
{
ui->setupUi(this);
ui->pushButtonOK->setEnabled(false);
QRegExp regexp("[A-Za-z][1-9][0-9]{0,2}");
ui->lineEditCellLocation->setValidator(new QRegExpValidator(regexp, this));
}
gotocellDialog::~gotocellDialog()
{
delete ui;
}
void gotocellDialog::on_lineEditCellLocation_textChanged(const QString &arg1)
{ui->pushButtonOK->setEnabled(ui->lineEditCellLocation->hasAcceptableInput());
}
here another file mainwindow creating new gotocell object but unable to access its(gotocell's) component lineEditCellLocation
void spMainWindow::gotocell()
{
gotocelldlg = new gotocellDialog(this);
if(gotocelldlg->exec())
{
QString str = gotocelldlg->lineEditCellLocation->text.toUpper();
spsheet->setCurrentCell(str.mid(1).toInt() - 1,
str[0].unicode() - 'A');
}
}
here error is showing as :-
/opt/project/Qt/spreadsheet/spmainwindow.cpp:81: error: 'class gotocellDialog' has no member named 'lineEditCellLocation'
/opt/project/Qt/spreadsheet/spmainwindow.cpp:-1: In member function 'void spMainWindow::sort()':
here i want to access gotocelldialog's lineEditCellLocation but unable to get access.
I think, that this is just a small typo:
Replace the following line
QString str = gotocelldlg->lineEditCellLocation->text.toUpper();
with
QString str = ui->lineEditCellLocation->text.toUpper();
For your next posts you should include a minimal reproducible examples. This also includes the post of your ui file. Otherwise, it is very difficult for others to reproduce your problem.
finally i got its solution i did like this :-\
gotocelldlg = new gotocellDialog(this);
if(gotocelldlg->exec())
{
QLineEdit *ledit = findChild<QLineEdit*>("lineEditCellLocation");
QString str = ledit->text().toUpper();
spsheet->setCurrentCell(str.mid(1).toInt() - 1,
str[0].unicode() - 'A');
}
Related
I am making a simple browser using QT5.
I have a QMainWindow with a QWebEngineView inside of it and I am trying to make it so that it auto accepts permission requests but I can't seem to get it to work... (Later I will make it prompt the user)
I looked online and found something but the solution didn't work for me as they set their application up differently than I did.
The main.cpp is simply the default with a declaration of MainWindow and showing it
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUrl>
#include <QWebEngineView>
#include <QWebEnginePage>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->view->page(), ui->view->page()->featurePermissionRequested, this, this->onFeaturePermissionRequested);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_lineEdit_returnPressed()
{
QString url = ui->lineEdit->text();
QUrl curl;
curl.setUrl(url);
ui->view->setUrl(curl);
}
void MainWindow::on_back_pressed()
{
ui->view->back();
}
void MainWindow::on_forward_pressed()
{
ui->view->forward();
}
void MainWindow::on_reload_pressed()
{
ui->view->reload();
}
void MainWindow::on_view_urlChanged(const QUrl &arg1)
{
QString newurl = arg1.toString();
QStringList urllist = newurl.split('?');
ui->lineEdit->setCursorPosition(0);
ui->lineEdit->setText(urllist[0]);
}
void MainWindow::onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature)
{
ui->view->page()->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionPolicy(1));
}
And the header file is as so:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWebEnginePage>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_lineEdit_returnPressed();
void on_back_pressed();
void on_forward_pressed();
void on_reload_pressed();
void on_view_urlChanged(const QUrl &arg1);
void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Whenever I attempt to build it I get these messages
/home/kiwifruit555/Documents/kUwU/Web/mainwindow.cpp:12: error: invalid use of non-static member function ‘void QWebEnginePage::featurePermissionRequested(const QUrl&, QWebEnginePage::Feature)’
../Web/mainwindow.cpp: In constructor ‘MainWindow::MainWindow(QWidget*)’:
../Web/mainwindow.cpp:12:49: error: invalid use of non-static member function ‘void QWebEnginePage::featurePermissionRequested(const QUrl&, QWebEnginePage::Feature)’
12 | connect(ui->view->page(), ui->view->page()->featurePermissionRequested, this, this->onFeaturePermissionRequested);
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/qt/QtWebEngineWidgets/QWebEnginePage:1: In file included from /usr/include/qt/QtWebEngineWidgets/QWebEnginePage:1,
In file included from /usr/include/qt/QtWebEngineWidgets/QWebEnginePage:1,
from ../Web/mainwindow.h:5,
from ../Web/mainwindow.cpp:1:
/usr/include/qt/QtWebEngineWidgets/qwebenginepage.h:342:10: note: declared here
342 | void featurePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
I have tried multiple things but I cannot seem to get it to work...
The connection syntax should be:
connect(ui->view->page(), &QWebEnginePage::featurePermissionRequested, this, &MainWindow::onFeaturePermissionRequested);
Fore more information read New Signal Slot Syntax.
It is also better to use the enum value explicitly instead of the numeric value:
ui->view->page()->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
I'm writing QT deskop app and I have a small issue.
I want to open new tab with text that I put from a file.
I have two class one in mainwindow.h and second in form.h
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void on_open_file_clicked();
void on_search_keyword_clicked();
void on_search_tag_clicked();
void on_tabWidget_tabCloseRequested(int index);
void show_tab(QString keywords);
QString return_text();
public:
Ui::MainWindow *ui;
private slots:
void on_actionOpen_file_triggered();
void on_actionShow_text_file_triggered();
QVector<QString>find_logs_keywords(QString keywords);
private:
QString text = "example text";
};
#endif // MAINWINDOW_H
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
public slots:
void on_pushButton_2_clicked();
void text_to_plain(QString& text);
private:
Ui::Form *ui;
};
#endif // FORM_H
mainwindow.cpp
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QDir>
#include <QVector>
#include <QStringList>
#include <iostream>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "search_keyword.h"
#include "search_tag.h"
#include "form.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_open_file_clicked()
{
QString filter = "log file (*log) ;; Tex File (*.txt)";
QString file_name = QFileDialog::getOpenFileName(this, "Open a file", QDir::homePath(), filter);
QMessageBox::information(this,"..",file_name);
QFile file(file_name);
if (!file.open(QFile::ReadOnly | QFile::Text)) QMessageBox::warning(this,"title","file not open");
else
{
QTextStream in(&file);
text = in.readAll();
ui->plainTextEdit->setPlainText(text);
ui->lineEdit->setPlaceholderText(file_name);
}
file.close();
}
void MainWindow::on_actionShow_text_file_triggered()
{
show_tab("all logs");
Form Fr;
Fr.text_to_plain(text);
}
void MainWindow::show_tab(QString keywords)
{
ui->tabWidget->addTab(new Form(), QString("%1").arg(keywords));
ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
}
form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
void Form::text_to_plain(QString& text)
{
ui->plainTextEdit->setPlainText(text);
}
My problem is after call method void MainWindow::on_actionShow_text_file_triggered() plain text in QWidget in Form is still empty i tried: this->update, this->repaint, Form::update(); Form::repaint(), QWidget::repaint(); QWidget::update();. Everything fail.
the problem is that your Form exists only in this function scope.
void MainWindow::on_actionShow_text_file_triggered()
{
show_tab("all logs");
Form Fr;
Fr.text_to_plain(text);
}
Here -> Form Fr; the Form is created: constructor(explicit Form(QWidget *parent = nullptr);) of the class Form is called
And at this place -> } your Form is deleted: destructor(~Form();) of class Form is called.
This happens because you create your Form instance on the function stack. So Fr is local object and exist only in the scope of the function on_actionShow_text_file_triggered() after the function is executed/finished all local variables are freed/destructed.
if your Fr object should exist not just in this function you need to create it on the heap instead of stack.
P.S.: for case study read about stack vs heap for example hear: What and where are the stack and heap?
Your on_actionShow_text_file_triggered function should then look like this:
void MainWindow::on_actionShow_text_file_triggered()
{
show_tab("all logs");
Form * Fr = new Form(this); //if you set parent object your Form will be automatically deleted/destructed then MainWindow is deleted/destructed
Fr->text_to_plain(text);
Fr->show(); // you should call show() function of QWidget class and subclasses to get your widget visible
}
This Form * Fr = new Form(this); will create new form pointer every time. Would not it be better to create one global pointer to the form, call the functions and delete it later?
I'm using Qt 5 on a Windows and building a GUI App with multiple QDialog classes. I am trying to connect a signal from a QDialog in a triggered action of the QMainWindow class after instances of both have been created. I have read the documentation on Qt here: http://doc.qt.io/qt-4.8/signalsandslots.html and here: https://wiki.qt.io/New_Signal_Slot_Syntax. I have also read through many questions on stackoverflow that have helped correct some of the initial errors I was getting, but haven't helped me solve this problem.
The error I keep getting is:
"expected primary-expression before ',' token"
I have tried both the old syntax for connect
connect(sender, SIGNAL (valueChanged(QString,QString)),
receiver, SLOT (updateValue(QString)) );
and the new syntax (which is shown in the .cpp file below)
connect(sender, &Sender::valueChanged,
receiver, &Receiver::updateValue );
The MainWindow is created in the main.cpp and the 2nd dialog is created on_action_someAction_triggered(), so I know that the instances I am referencing exist. Is there a better way for me to connect the SIGNAL and the SLOT?
Here is the code I am working with (minus the extra unrelated code).
mainwindow .h:
#include <QMainWindow>
#include "shipdia.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void loadSelectedShip(QString shipName);
private slots:
void on_actionNew_Ship_triggered();
private:
Ui::MainWindow *ui;
shipdia *sDialog;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextStream>
#include <QObject>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionNew_Ship_triggered()
{
sDialog = new shipdia(this);
QObject::connect(&shipdia, //this is were I attempt to
&shipdia::sendShip, //connect the signal/slot
this,&MainWindow::loadSelectedShip); //but always get an error
sDialog ->show();
}
void MainWindow::loadSelectedShip(QString shipName)
{
... do something ... //this code works, but the signal is never received
}
qdialog.h
#ifndef SHIPDIA_H
#define SHIPDIA_H
#include "functions.h"
#include <QDialog>
namespace Ui {
class shipdia;
}
class shipdia : public QDialog
{
Q_OBJECT
public:
explicit shipdia(QWidget *parent = 0);
~shipdia();
private slots:
void on_pushButton_2_clicked();
signals:
void sendShip(QString shipName);
private:
Ui::shipdia *ui;
};
#endif // SHIPDIA_H
qdialog.cpp
#include "shipdia.h"
#include "ui_shipdia.h"
#include <QObject>
#include <QMessageBox>
#include <QTextStream>
#include <QDir>
shipdia::shipdia(QWidget *parent) :
QDialog(parent),
ui(new Ui::shipdia)
{
ui->setupUi(this);
}
shipdia::~shipdia()
{
delete ui;
}
void shipdia::sendSelectedShip(QString shipName)
{
emit sendShip(shipName); //I previously just emitted sendSelectedShip,
//but separating the function did not fix it.
}
void shipdia::on_pushButton_2_clicked()
{
//Code below functions up to next comment
QString shipName = ui->line_23->text();
shipName = QDir::currentPath() + "/shipFolder/" + shipName + ".txt";
QFile shipFile(shipName);
QStringList stringList;
if (shipFile.open(QIODevice::ReadOnly))
{
QTextStream in(&shipFile);
while(!in.atEnd())
{
QString line = in.readLine();
if(line.isNull())
break;
else
stringList.append(line);
}
shipFile.close();
}
//Code above functions ^
sendSelectedShip(shipName); //this line does not produce an error
}
I think, the code should be
sDialog = new shipdia(this);
QObject::connect(sDialog,
&shipdia::sendShip,this,&MainWindow::loadSelectedShip);
and it should be placed in the constructor of the MainWindow, right after ui->setupUi(this); and the on_actionNew_Ship_triggered() function should look like this:
void MainWindow::on_actionNew_Ship_triggered()
{
sDialog ->show();
}
In your original code, a new instance of shipdia will be created everytime the on_actionNew_Ship_triggered() is called. That should be avoided.
Hope this helps.
I'm trying to understand Qt 4.8 signals and slots so I wrote some code to test it out for myself. Eventually, I want to be able to use a common source file in my project so that serial ports can be accessed from any source file in the project.
I set up a Qt GUI application and added a C++ class header and source file, shown below.
When I try to build, I get the error message when I try to emit the signal.
/home/user/QTProjects/stest1/stest1/ser.cpp:25: error: invalid use of 'this' in non-member function
I haven't even gotten to the stage of setting up the connections yet!
My newbie status is obvious, I'd be grateful for any help.
Thanks,
James
The following is the MainWindow.cpp:-
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ser.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ser *j = new ser;
j->init();
connect (this, SIGNAL(click()), ser, SLOT(testprint()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QByteArray ba1;
ba1.resize(6);
ba1[0]='H'; ba1[1]='e'; ba1[2]='l'; ba1[3]='l'; ba1[4]='o'; ba1[5]='\n';
this->printtext(ba1);
}
void MainWindow::printtext(const QByteArray &data)
{
ui->textEdit->insertPlainText(QString(data));
}
The following is the MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void printtext(const QByteArray &data);
private:
Ui::MainWindow *ui;
signals:
// void click;
};
#endif // MAINWINDOW_H
The following is ser.cpp:-
#include "ser.h"
#include <QObject>
ser::ser(QObject *parent) :
QObject(parent)
{
}
void ser::init()
{
// connect(this->, SIGNAL(testsignal), MainWindow, SLOT(printtext(const QByteArray &data)));
}
void ser::testprint()
{
QByteArray ba1;
ba1.resize(8);
ba1[0]='S'; ba1[1]= '0'; ba1[2]= ' '; ba1[3]= 'l'; ba1[4]='o'; ba1[5]='n'; ba1[6]='g'; ba1[7]='\n';
emit this->testsignal(ba1);
}
The following is ser.h
#ifndef SER_H
#define SER_H
#include "mainwindow.h"
#include <QObject>
class ser : public QObject
{
Q_OBJECT
public:
explicit ser(QObject *parent = 0);
void init();
signals:
void testsignal(const QByteArray &data);
private slots:
void testprint();
public slots:
};
#endif // SER_H
Your method is implemented as void testprint() { ... }, but it should be void ser::testprint() { ... }. It's in your cpp file.
Also note that you don't need to use this-> to refer to class members. emit testsignal(ba1); will fork fine.
I think should be
connect (this, SIGNAL(click()), j, SLOT(testprint()));
instead of
connect (this, SIGNAL(click()), ser, SLOT(testprint()));
that apart, I can't spot where you connect testsignal
Great, that worked.
connect (this, SIGNAL(click()), j, SLOT(testprint()));
My next problem is connecting the signal in ser to the slot in the MainWindow. I used
connect(j,
SIGNAL(testsignal),
this,
SLOT(printtext(const QByteArray &data)));
It was inserted immediately after the other connect statement.
This does not print out the expected message "Slong". It also does not give me any error! What is the problem?
James
I have a QTabWidget which contains a QPlainTextEdit. I have managed to add action to the QTabWidget so that whenever a new tab opens, a new QPlainTextEdit is also added in the new tab. See code.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPlainTextEdit>
#include <QMessageBox>
#include <QAction>
#include <QTextCursor>
#include <iostream>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("Tilde");
current_tab = 1;
on_action_New_triggered();
ui->tabWidget->setTabsClosable(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_action_New_triggered()
{
QString newTab = "Tab " + QString::number(current_tab);
ui->tabWidget->addTab(new QPlainTextEdit, newTab);
ui->tabWidget->setCurrentIndex(current_tab - 1);
current_tab++;
editor = qobject_cast<QPlainTextEdit *>(ui->tabWidget->currentWidget());
editor->setFocus();
/*connect(editor->document(), SIGNAL(cursorPositionChanged(QTextCursor)),
this, SLOT(on_editor_cursorPositionChanged()));*/
}
void MainWindow::on_actionNew_document_triggered()
{
on_action_New_triggered();
}
void MainWindow::on_action_Exit_triggered()
{
QMessageBox msg;
msg.addButton(QMessageBox::Yes);
msg.addButton(QMessageBox::No);
msg.setText("Exit program?");
int selection = msg.exec();
if (selection == QMessageBox::Yes)
qApp->exit(0);
}
// highlight current line
void MainWindow::on_editor_cursorPositionChanged()
{
QTextEdit::ExtraSelection highlight;
highlight.cursor = editor->textCursor();
highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
highlight.format.setBackground( QColor(240, 246, 217) );
QList<QTextEdit::ExtraSelection> extras;
extras << highlight;
editor->setExtraSelections(extras);
}
The commented code gives compiler error:
QMetaObject::connectSlotsByName: No matching signal for
on_editor_cursorPositionChanged()
I have added the function in the header file.
Header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPlainTextEdit>
#include <QTextCursor>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_action_New_triggered();
void on_actionNew_document_triggered();
void on_action_Exit_triggered();
void on_editor_cursorPositionChanged();
private:
Ui::MainWindow *ui;
QPlainTextEdit *editor;
qint8 current_tab;
};
#endif // MAINWINDOW_H
Could it be that your signature for the SLOT is wrong?
/*connect(editor->document(), SIGNAL(cursorPositionChanged(QTextCursor)),
this, SLOT(on_editor_cursorPositionChanged()));*/
Should be?
connect(editor->document(), SIGNAL(cursorPositionChanged(QTextCursor)),
this, SLOT(on_editor_cursorPositionChanged(QTextCursor)));
Also, the naming convention you are using for that slot might be conflicting here with your manual connection. Qt may be trying to use the connectSlotsByName mechanism on your SLOT by matching the name: on_<member>_<signal>
In this case, the current signature of that SLOT on_editor_cursorPositionChanged() would match with the QPlainTextEdit editor member. And then you are manually connecting the document to it with the wrong signature. You probably should create another slot that is named more normally docCursorPosChanged(QTextCursor)