Qt C++: Unable to render Vertical Headers - c++

I've got a custom model derived from QAbstractItemModel.
data, columnCount,rowCount,parent,index,setData,flags,headerData and setHeaderData have all been overrided.
flags only returns Qt::ItemIsEditable (for future)
But the problem starts with headerData(), it seems messages with Qt::Vertical as orientation never reaches it. So, to my understanding, there can't be any vertical headers.
The code serves well for horizontal though:
QVariant MiModeloCalendarioGeneral::headerData(int section, Qt::Orientation orientation, int role) const
{
qDebug() << "{";
qDebug() << "Role: " << role;
qDebug() << "Section: " << section;
qDebug() << "Orientation: " << orientation;
qDebug() << "}";
if (role != Qt::DisplayRole) {
return QVariant();
}
if (orientation == Qt::Horizontal) {
qDebug() << "Horizontal";
return fileras[section];
}
if (orientation == Qt::Vertical) {
qDebug() << "Vertical";
return cabeceras[section];
}
}
What am I missing?
I've checked that both 'cabeceras' and 'fileras' have content. And the setHeaderData function does emit the headerDataChanged signal.
EDIT: Typo

Related

how to use QCanbusDevice set canfd

if (QCanBus::instance()->plugins().contains(QStringLiteral("vectorcan"))) {
QString errorString;
QCanBusDevice *device = QCanBus::instance()->createDevice(
QStringLiteral("vectorcan"), QStringLiteral("can0"), &errorString);
if (!device) {
// Error handling goes here
qDebug() << errorString;
} else {
device->configurationParameter(QCanBusDevice::CanFdKey).toInt();
device->setConfigurationParameter(QCanBusDevice::BitRateKey, QVariant::fromValue(250000));
device->setConfigurationParameter(QCanBusDevice::CanFdKey, QVariant(true));
qInfo() << "connect parameter:" << device->configurationParameter(QCanBusDevice::BitRateKey).toInt() <<
device->configurationParameter(QCanBusDevice::CanFdKey).toInt();
QCanBusFrame frame;
frame.setFrameId(510);
QByteArray payload("4010D113123");
frame.setPayload(payload);
qInfo() << device->writeFrame(frame);
}
}**strong text**
I have tried to use setConfigurationParameter API to change the canfd,but it doesn't work. And this api can change the bitrate parameter. I don't kown why it doesn't work.

eventFilter(QObject *obj, QEvent *e) does not detect right object

I expect my program to print "mouse on label name" when my mouse is located on the labelname (a QLabel), and to print"mouse not on label name" when my mouse is not located on the labelname.
Even though I put my mouse on labelname, my program prints "mouse not on label name".
How can I know when my mouse is not on the labelname?
bool Dialog::eventFilter(QObject *obj, QEvent *e)
{
if(qobject_cast<QLabel*>(obj) == ui->labelname) {
cout << “mouse on label name” << endl;
}else if(qobject_cast<QLabel*>(obj) != ui->labelname) {
cout << “mouse not on label name” << endl;
}
return false;
}
Be sure you are installing the event filter correctly. Also, if you want to track mouse position you have to enable mouseTracking, otherwise the move events won't be triggered (though QEvent::Enter and QEvent::Leave will, which are the ones that indicates the mouse has entered or left the widget).
Here a minimal example of how to do it:
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
m_label = new QLabel("Hello world!");
m_label->setObjectName("m_label");
m_label->installEventFilter(this);
m_label->setMouseTracking(true);
auto hlayout = new QVBoxLayout();
hlayout->addWidget(m_label);
setLayout(hlayout);
}
bool MyWidget::eventFilter(QObject* sender, QEvent* event)
{
if (sender == m_label) {
qDebug() << sender->objectName() << event->type();
if (event->type() == QEvent::Enter) {
qDebug() << "mouse on label name";
} else if (event->type() == QEvent::Leave) {
qDebug() << "mouse not on label name";
}
}
return QWidget::eventFilter(sender, event);
}
The full working example is available in GitHub.

Qt Creator - Remove all UI keyboard shortcuts (QButton, QSlider etc.)

I was wondering if there was a way I could remove all keyboard shortcuts from the UI so I can use the event filter to grab key commands instead.
Currently, I have this in my eventFilter() function and it does the job but it also affects the QSliders, QLineEdits and QButtons I have on there - and I don't want it to.
bool LinkControl::eventFilter(QObject *o, QEvent *e)
{
QKeyEvent *keyEvent = NULL;
// qDebug() << "TYPE: " << e->type();
switch(e->type())
{
case 51:
// qDebug() << this->overrideControl;
if(this->overrideControl == true)
{
keyEvent = static_cast<QKeyEvent*>(e);
// this->handleKeyboardInput(true, keyEvent->key());
// emit handleInput(true, keyEvent->key());
this->handleKeyboardInput(true, keyEvent->key());
// qDebug() << "PRESS: " << keyEvent->key();
}
this->overrideControl = !this->overrideControl;
return true;
break;
case QEvent::KeyRelease:
keyEvent = static_cast<QKeyEvent*>(e);
// this->handleKeyboardInput(false, keyEvent->key());
// emit handleInput(false, keyEvent->key());
this->handleKeyboardInput(false, keyEvent->key());
// qDebug() << "RELEASE: " << keyEvent->key();
return true;
break;
default:
return QMainWindow::eventFilter(o, e);
}
}
For example, In my handleKeyboardInput function, I have the up arrow key increment a value and it does that but it also moves a QSlider if it's selected.
Thank you.

Qt keyPressEvent stops working when passing parent to the class

I am writing a keyboard tester, so I start a dialog with
keybDialog keyboardTestWindow;
keyboardTestWindow.setWindowTitle("Keyboard test");
keyboardTestWindow.exec();
override the keyPressEvent(QKeyEvent *event) and the test works superb. Now, I needed use of some parameters from parent, so instead I did
keybDialog keyboardTestWindow(m_parent);
keyboardTestWindow.setWindowTitle("Keyboard test");
keyboardTestWindow.exec();
and implemented what I needed. Now I discovered that just by instantiating the class and passing the parent to it, my dialog no longer receives the keyPressEvent. Are these passed to m_parent in some way? How can I avoid this?
EDIT:
The beginning of my keyPressEvent:
void keybDialog::keyPressEvent(QKeyEvent *event)
{
ui->txtBxKeyboardInput->clear();
qDebug() << "Event: " << hex << event->key() << event->nativeVirtualKey() << event->modifiers() << event->nativeModifiers() << event->nativeScanCode();
ui->txtBxKeyboardInput->setText(ui->txtBxKeyboardInput->text()+event->text());
switch(sequenceNumber)
{
case 0: // Left Shift + R
if( (event->key() == Qt::Key_R) && event->modifiers() == Qt::ShiftModifier && event->nativeModifiers() == 0x201)
{
reportSuccessfullKey(sequenceNumber);
}
else
{
ui->lblStatus->setText("WRONG");
}
break;

How to maintain specific height to width ratio of widget in qt5?

I tried overriding methods hasHeightToWidth() and heightToWidth() but it didn't work for some reason.
Is there some complete example that I can use?
Upd1:
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget() {
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
sizePolicy.setHeightForWidth(true);
setSizePolicy(sizePolicy);
}
bool hasHeightForWidth() const override {
std::cout << __FUNCTION__ << std::endl;
return true;
}
int heightForWidth(int w) const override {
std::cout << __FUNCTION__ << " " << w << std::endl;
return w;
}
QSize sizeHint() const override {
return QSize(100, heightForWidth(100));
}
};
MyWidget instances are inserted in QHBoxLayout.
I use qt5.
Debug std::cout's show that hasHeightForWidth and heightForWidth are called many times