Creating a keypress signal from application button
-
Could this be a possible structure?
http://imageshack.us/photo/my-images/99/possibleguiprogramstruc.png
Or would it be possible to subclass both QWidget and QTableView in Parent_ui? I'm having a hard time figuring this out :(
EDIT: Hm - Just tried creating a separate class for holding my slot like my diagram above shows.
@void Test_class::keyDownSlot(QTableView widget) {
widget.moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier);}
@I guess I'm still far away from a solution :(
-
I'm sorry, but that picture does not say much to me... Looks like you are using Qt Designer to design your widgets, which is something I have never done (and hopefully never will do :). So someone else will have to help you with that.
Regarding multiple inheritance, you cannot inherit from two QObject subclasses, so that's not an option.
I don't know what you are using your QWidgets for, but maybe what you want to do is something like this:
Create a subclass of QTableView, e.g. MyTableView, which has a couple of new slots moveUp(), moveDown() etc.
Create a subclass of QWidget, MyWidget that has a layout and contains some buttons and an instance of MyTableView.
Connect the clicked() signals of the buttons to the corresponding signals in the MyTableView instance, e.g. in the MyWidget constructor.
Hope this helps.
-
Thank you so much ludde!
What you describe is almost what I sketch in my diagram. The only difference I guess is that I had all my UI's have an instance of my QTableView subclass. Of course it should be MyWidget (In my case Parent_ui) that should have the instance of the QTableView subclass.
I will try this out!
Thank you for your time!
EDIT: Come to think about it, in my case the subclasses of Parent_ui needs the instance of MyTableView. This is because it is here the buttons and QTableView is created.
-
I get this error when trying to build:
error: 'QTableView& QTableView::operator=(const QTableView&)' is private
The header file of MyTableView looks like this:
@#include "qtableview.h"
class MyTableView: public QTableView {
Q_OBJECT;
public:
MyTableView(QWidget* parent = 0);
virtual ~MyTableView();
public slots:
void key_down();
void key_up();};@
Do I need to change something here? Or does it look right?
-
First, not related with your problem things:
- ';' not needed after Q_OBJECT - it's macros
- 'virtual' keyword for destructor not needed in your class, because it's already defined as virtual in base class.
Second, your error:
Can you show line where error was happen?
For subclasses of QObject assignment operator and copy constructor always are private. -
- Okay - I'll correct that.
- When I remove the virtual keyword I get a warning:
"Class "MyTableView" has virtual method 'qt_metacall' but non-virtual destructor"
- I get the error in line 3 in the code showed in my previous post. I will add the code again, with the changes you mentioned.
@#include "qtableview.h"
class MyTableView: public QTableView {
Q_OBJECT
public:
MyTableView(QWidget* parent = 0);
~MyTableView();
public slots:
void key_down();
void key_up();};@
Thank you for your time.
-
My setup.h file:
@#include "ui_setup.h"
#include "parent_ui.h"
#include "mytableview.h"class Setup: public Parent_ui {
Q_OBJECTpublic:
Setup(QWidget *parent = 0);
~Setup();
MyTableView myTableViewInstance;private:
Ui::SetupClass ui;
void createActions();private slots:
void up();
void down();
void enter();
void back();};@
My setup.cpp file:
@#include "setup.h"
#include <QListWidget>Setup::Setup(QWidget *parent) :
Parent_ui(parent) {ui.setupUi(this);
Parent_ui::ui.headerLabel->setText("SETUP");myTableViewInstance = new MyTableView();
}Setup::~Setup() {
}
@The error I get when calling "myTableViewInstance = new MyTableView();":
'QWidget' is an inaccessible base of 'MyTableView'
-
That was it, thank you :)
Okay to get back to the subject (Which I slowly turned away from introducing other challenges :) ).
I have made a slot in MyTableView:
@void MyTableView::key_down(){
QTableView::moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier);
}@This is then used in my setup.cpp file:
@void Setup::createActions() {
QObject::connect(ui.buttonTwo, SIGNAL(clicked()), this, SLOT(myTableViewInstance->key_down()));
}@
This has no effect on my QTableWidget though. But I guess it's me and my coding which is faulty again. Can you see from my code snippets what I'm doing wrong?
-
Oh... many points:
- In your sources
@class MyTableView: private QTableView {@
although, in example above you wrote:
@class MyTableView: public QTableView {@- You don't needed MyTableView, you already have QTabletWidget on form
- You don't needed inheritance in this case.
- In setup.cpp
I rewrite some things:
It's for local slot, because MyTableView not needed anymore
@connect(ui.buttonTwo, SIGNAL(clicked()), this, SLOT(down()));@
in slot, as example, you can add any checks and other logic to their:
@void Setup::down() {
Parent_ui::ui.tableWidget->selectRow(Parent_ui::ui.tableWidget->currentRow() + 1);
}@same for up slot:
@void Setup::up() {
Parent_ui::ui.tableWidget->selectRow(Parent_ui::ui.tableWidget->currentRow() -1);}@
- In your sources