Qt need help to disable scroll on combobox - qt

Hello I need help to turn off scrool on combobox , I have written some code but I get error, can someone help me
list << ui->comboBox << ui->comboBox_2 << ui->comboBox_3;
for(i =0 ; i<list.count();i++)
{
list[i]->installEventFilter(this);
list[i]->setFocusPolicy(Qt::StrongFocus);
}
bool MainWindow::eventFilter(QObject * o,QEvent * e)
{
if(e->type() == QEvent::Wheel && o == list.at(i) )
{
return true;
}
return false;
}
When i run program i get this Error
ASSERT failure in QList::at: "index out of range"

start in debug mode see where is the error.
solution: remove o == list.at(i)

Related

Loop program if user enters wrong input

I just started learning C# and while loops are confusing me. Unlike Java, where I can use a while loop to loop a program if a user entered a invalid input, it's not acting the same way in C#.
using System;
namespace first {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hi! What is your name");
string userName = Console.ReadLine();
Console.WriteLine("oh! you are:" + userName);
Console.WriteLine("let play a game");
string answer="Y";
while (answer == "Y") {
Random random = new Random();
int correntNumber = random.Next(1, 2);
int guess = 0;
Console.WriteLine("Guess a number");
while (guess != correntNumber) {
string userGuess = Console.ReadLine();
//validate input method 1
try {
guess = int.Parse(userGuess);
} catch (Exception e) {
Console.WriteLine("Invalid inout", e);
}
//validate input method 2
//if(!int.TryParse(userGuess, out guess)) {
// Console.WriteLine("invalid input");
//}
if (guess != correntNumber) {
Console.WriteLine("try again!");
}
}
Console.WriteLine("Yes! corrector");
Console.WriteLine("Play again?");
//string answer;
answer = Console.ReadLine().ToUpper();
if(answer == "Y") {
continue;
} else if (answer == "N") {
Console.WriteLine("bye");
return;
} else if (answer != "Y" || answer != "N") {
Console.WriteLine("y or n");
answer = Console.ReadLine().ToUpper();
continue;
}
}
}
}
}
When I enter a value other than y or n, the message appears,Console.WriteLine("Y or n only");, but the game restarts while it shouldn't.
I am sorry this is a simple and rather silly question, but I can't pin point where I am going wrong.
the problem is that after printing to the user "y or n only" message you take the input but you don't actually do anything with it
so the loop just restarts regardless of the input , to fix this issue you could replace the last if part with this code
while(answer != 'Y' && answer != 'N'){
Console.WriteLine("y or n only");
answer = Convert.ToChar(Console.ReadLine().ToUpper());
}
if(answer == 'Y')
{
continue;
}
else if(answer == 'N')
{
Console.WriteLine("goodbye");
return;
}
so after you read the first input answer of him for repeating or no you check if it's a valid input or not and if it's not you keep asking him for "y or n only" till he enters "Y" or "N" and then you process this answer for whether it's a "Y" or "N" in the if part

How to use active() method x++

Ok I did it. It works fine. Thanks for help. Here is my code. Now I only need to call my command button in a differend form to disable it and create a info there. Anyone could look about it ? In my code I got reference errors.
[ExtensionOf(formdatasourcestr(ProdTableListPage, ProdTable))]
final class ProdParmReportFinishedActiveWG_Extension
{
public int active()
{
int ret;
next Active();
{
ProdTable tableBuffer = this.cursor();
ProdTable prodtable;
if(tableBuffer.ProdId == tableBuffer.CollectRefProdId
&& tableBuffer.ProdStatus != ProdStatus::ReportedFinished)
{
select firstonly RecId,ProdId from ProdTable where
ProdTable.CollectRefProdId == tableBuffer.ProdId
&& ProdTable.Prodstatus != ProdStatus::ReportedFinished
&& tableBuffer.RecId != prodtable.RecId;
{
Global::info(strFmt("%1 , %2",
prodtable.prodid, prodtable.recid));
// FormButtonControl mybutton = this.FormRun().design().controlname(formControlStr(ProdParmReportFinished, Ok)) as FormButtonControl;
// mybutton.enabled(false);
}
}
else
{
Global::info(strFmt("%1 , %2, %3, %4",
tableBuffer.prodid, tableBuffer.CollectRefProdId, tableBuffer.InventRefType, tableBuffer.ProdStatus));
}
}
return ret;
}
}
"I want to use this code everytime user changes his actual row but instead it runs just once and apply to all my rows."
Use the selectionChanged() method instead of active().
In fact most use cases where you think you should use active(), you're probably looking for selectionChanged() (or the OnSelectionChanged event for handlers) instead.

Blocking a specific input device in Xorg apps, using xcb_wait_for_event only

There is a bug in Qt in which multiple touch screens causes Qt to be in an inconsistent state. More about the issue here.
As a short-term patch, I'd to use event filters (which Qt provides for xcb events) to prevent multiple devices from being processed by Qt at a time.
The steps would be as followed.
A sequence of events for input is beginning (mouse button down, touch press, etc).
Block all other events for devices that don't belong to the current device being used.
When the sequence of events is completed, Resume events for all devices, starting back at step 1.
Effectively, I want to gate the events, so that only one device can be used at a time. I'm hoping this get's around Qt's bug.
First, I am trying to just filter events for a hard-coded device to see if this even gets around Qt's bug, but it doesn't.
class DuplicateHardwareEventFilter : public QAbstractNativeEventFilter
{
public:
DuplicateHardwareEventFilter() {}
bool nativeEventFilter(const QByteArray &eventType, void *message, long *) override
{
if (eventType == "xcb_generic_event_t") {
xcb_generic_event_t* ev = static_cast<xcb_generic_event_t *>(message);
uint responseType = ev->response_type & ~0x80;
if(responseType == XCB_GE_GENERIC) {
xcb_ge_event_t* gev = reinterpret_cast<xcb_ge_event_t*>(ev);
// assume input event
xcb_input_button_press_event_t* xiEvent = reinterpret_cast<xcb_input_button_press_event_t*>(ev);
if(xiEvent->event_type == XCB_INPUT_DEVICE_CHANGED) {
auto inputChangedEvent = reinterpret_cast<xcb_input_device_changed_event_t *>(gev);
if(inputChangedEvent->sourceid == 11) {
return true;
}
qDebug("xcb device changed: %d source: %d", xiEvent->deviceid, inputChangedEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_MOTION) {
auto inputMotionEvent = reinterpret_cast<xcb_input_motion_event_t*>(gev);
if(inputMotionEvent->sourceid == 11) {
return true;
}
qDebug("xcb motion: %d source: %d", inputMotionEvent->deviceid, inputMotionEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_ENTER) {
auto inputEnterEvent = reinterpret_cast<xcb_input_enter_event_t*>(gev);
if(inputEnterEvent->sourceid == 11) {
return true;
}
qDebug("xcb enter: %d source: %d", inputEnterEvent->deviceid, inputEnterEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_LEAVE) {
auto inputLeaveEvent = reinterpret_cast<xcb_input_leave_event_t*>(gev);
qDebug("xcb leave: %d source: %d", inputLeaveEvent->deviceid, inputLeaveEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_BUTTON_PRESS) {
auto buttonPressEvent = reinterpret_cast<xcb_input_button_press_event_t*>(gev);
qDebug("xcb buttonPress: %d source: %d", buttonPressEvent->deviceid, buttonPressEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_BUTTON_RELEASE) {
auto buttonReleaseEvent = reinterpret_cast<xcb_input_button_release_event_t*>(gev);
qDebug("xcb buttonRelease: %d source: %d", buttonReleaseEvent->deviceid, buttonReleaseEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_TOUCH_BEGIN) {
auto touchBeginEvent = reinterpret_cast<xcb_input_touch_begin_event_t*>(gev);
if(touchBeginEvent->sourceid == 11) {
return true;
}
qDebug("xcb touchBegin: %d source: %d", touchBeginEvent->deviceid, touchBeginEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_TOUCH_UPDATE) {
auto touchUpdateEvent = reinterpret_cast<xcb_input_touch_update_event_t*>(gev);
if(touchUpdateEvent->sourceid == 11) {
return true;
}
qDebug("xcb touchUpdate: %d source: %d", touchUpdateEvent->deviceid, touchUpdateEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_TOUCH_END) {
auto touchEndEvent = reinterpret_cast<xcb_input_touch_end_event_t*>(gev);
if(touchEndEvent->sourceid == 11) {
return true;
}
qDebug("touchEnd: %d source: %d", touchEndEvent->deviceid, touchEndEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_PROPERTY) {
auto propertyEvent = reinterpret_cast<xcb_input_property_event_t*>(gev);
qDebug("property: %d", propertyEvent->deviceid);
return false;
}
return false;
}
}
return false;
}
};
Qt still get's into it's quirky state.
How do I block a device completely, with filtering of the events of xcb_wait_for_event?
I do not grasp exactly what your issue is here, as I do not grasp what the inconsistent state does, but here is definitely a possible avenue if you get desperate.
For wayland, I wanted to create an API that would interface with my touchscreen, something like autohotkey or xdotools. Using the linux Kernel's uinput, I achieved remarkable success, interfacing with the device, and having it issue commands, and really vouch for how easy it was to get working. Just take a look here:
https://www.kernel.org/doc/html/v4.12/input/uinput.html
Definitely worth building an API, and perhaps even a virtual device driver.

Qt text editor project: auto-indenting when Enter is pressed after an opening curly brace

I'm working on a text editor project in Qt, and I've added the following method override to a class that's of type QPlainTextEdit:
/* Custom handler for events. Used to handle the case of Enter being pressed after an opening brace.
*/
bool Editor::eventFilter(QObject* obj, QEvent* event)
{
bool isKeyPress = event->type() == QEvent::KeyPress;
if(isKeyPress)
{
QKeyEvent *key = static_cast<QKeyEvent*>(event);
if(key->key() == Qt::Key_Enter || key->key() == Qt::Key_Return)
{
QString documentContents = document()->toPlainText();
if(documentContents.length() >= 1)
{
int indexToLeftOfCursor = textCursor().position() - 1;
if(indexToLeftOfCursor >= 0 && indexToLeftOfCursor < documentContents.length())
{
bool hitEnterAfterOpeningBrace = documentContents.at(indexToLeftOfCursor) == '{';
if(hitEnterAfterOpeningBrace)
{
// TODO determine indentation level of the opening brace
insertPlainText("\n\t\n}");
QTextCursor cursor = textCursor();
cursor.setPosition(cursor.position() - 3);
setTextCursor(cursor);
}
}
}
}
else
{
return QObject::eventFilter(obj, event);
}
}
else
{
return QObject::eventFilter(obj, event);
}
return false;
}
Without the code for setting the text cursor's position, the result looks like this:
The cursor ends up on line 4, and there's a tab on line 2. My intent is to move the cursor so it sits to the right of the tab, so I tried to do that with the code I've written. But that gives me this:
Here, the indent is still on line 2, but now there's an extra line between the tab and the closing brace, which is a little odd.
Even more strange is that this happens regardless of what new position I enter. I tried doing cursor.position() - 2 and cursor.position() - 1 out of curiosity, for example, but I still got the same result.
What am I misunderstanding here about inserting the text and moving the cursor?

how to determine if the mount successful in QT code

I have this function on mounting a smb:// connection. What if there is an error that is not in my condition. Is there a better way to determine if the mount is sucessful or not? Im using ubuntu 11.04 and qt 4.7.3
bool mwDM::mountFolder()
{
QString smbUsername,smbPassword,serverPath,mountPath;
QProcess connectSamba;
QString terminalCommand,linuxPassword;
QDir dir("/mnt/backup");
smbUsername=ReadINIStr(iniPath,"Server","Username","");
smbPassword=ReadINIStr(iniPath,"Server","Password","");
serverPath=ReadINIStr(iniPath,"Server","Hostname","");
serverPath="//" + serverPath;
mountPath="/mnt/backup";
linuxPassword=ReadINIStr(iniPath,"Server","AdminPassword","");
terminalCommand="echo "+linuxPassword+" | sudo -S mount -t cifs -o username="+smbUsername+",password="+smbPassword+" "+serverPath+ " "+mountPath;
connectSamba.start("sh",QStringList() << "-c" << terminalCommand );
if(!connectSamba.waitForStarted())
{
LogWrite("Failed to start mount command", Qt::red);
}
if(!connectSamba.waitForFinished() )
{
LogWrite("Failed to finish mount command", Qt::red);
}
QString connectSamba_stderr = connectSamba.readAllStandardError();
qDebug() << "connectSamba_stderr" << connectSamba_stderr;
if(connectSamba_stderr.contains("is not a valid block device"))
{
LogWrite("Hostname is invalid", Qt::red);
return false;
}
else if(connectSamba_stderr.contains("3 incorrect password attempts"))
{
LogWrite("Admin password is incorrect", Qt::red);
return false;
}
else if(connectSamba_stderr.contains("wrong fs type, bad option, bad superblock on"))
{
LogWrite("Hostname is invalid", Qt::red);
return false;
}
else if(connectSamba_stderr.contains("Invalid argument"))
{
LogWrite("Mount error(22): Invalid argument", Qt::red);
return false;
}
else if(!dir.exists())
{
LogWrite("Directory doesn't exists", Qt::red);
return false;
}
else
{
return true;
}
}
You can check last error of a QProcess by using error and state functions (Documentation for "error" Documentation for "state").
"What if there is an error that is not in my condition."
You can add something like this in your code:
else if(connectSamba.state() == QProcess::NotRunning && connectSamba.error() >= 0)
{
LogWrite("Unknown error", Qt::red);
return false;
}
Or, if you want to give more specific information, you can create a condition for each error code separately. Here's a list of codes.
Alternatively, don't add the above block to the error checking code. Instead, create a slot to which you connect the error signal of connectSamba class:
// add this line below "QProcess connectSamba;" line in mwDm::mountFolder
connect(&connectSamba, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError(QProcess::ProcessError));
// after that, use your original error checking code in mountFolder
// slot code
void mwDm::onError(QProcess::ProcessError)
{
//use switch-case or if to check type of error if you want
processErrorOccurred = true; // processErrorOccurred is a member of mwDm
}

Resources